开发者

Say I have a RabbitMQ queue, and I want to write clojure code to process the messages on the queue

开发者 https://www.devze.com 2023-03-09 07:17 出处:网络
How or in what way would I call out to my clojure code to process the tasks on a RabbitMQ queue? W开发者_开发技巧ould I have to write a daemon or what?

How or in what way would I call out to my clojure code to process the tasks on a RabbitMQ queue?

W开发者_开发技巧ould I have to write a daemon or what?

I know java code takes a long time to start up initially, so I would hope there would be a way to write a long running process for my clojure code to be run against.

I'm totally lost as to what container clojure would run in e.g. would it be a daemon, if yes, how to write a clojure daemon?


Your question is a bit generic, so let me break it down:

  1. You have a RabbitMQ queue and you want to consume messages from within Clojure
  2. You want to run this message consumer as a standalone program
  3. You want to run this program as a background task

From your question I understand that you've got the first part (consuming RabbitMQ messages in clojure) covered.

For the second part of your question, running a clojure program standalone:

You need to create a main function in your clojure code so that you can run it as a standalone program:

(defn -main [& args]
  "your code")

For more on this, see the clojure website. If you are using Leiningen as a build tool, you should specify your main function in your project.clj file and then build an uberjar, e.g.

$ lein compile
$ lein uberjar
$ java -jar my-uber-jar.jar

The procedure to run your program as a background task is different for different operating systems. The simplest way to run something in the background is to add an ampersand after the command:

$ java -jar my-uber-jar.jar &

But your program will terminate when you close the terminal you typed this command in.


I wrote a blog post about clojure and rabbitMQ, hope it helps!

It covers adding messages to and getting them from queues.

http://www.learningclojure.com/2011/02/rabbitmq-clojure-hello-world.html

If you don't know how to make daemons (It's quite complicated, and I've never done it from clojure), but want a long running process, then a work around might be a screen session. Then you can run your program in a terminal, but still log out and leave it running.

As it happens, I also once wrote a getting started tutorial about screen. You can find it here:

http://johnsunixtips.blogspot.com/2010/12/most-basic-possible-screen-tutorial.h

0

精彩评论

暂无评论...
验证码 换一张
取 消