JMS 客户端独立 Java 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6892791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JMS Client Standalone Java Program
提问by Vicky
I would like to use a standalone java program to poll and retrieve messages from JMS queue instead of having an MDB. Is it possible?
我想使用独立的 java 程序从 JMS 队列中轮询和检索消息,而不是使用 MDB。是否可以?
If it is, would it be possible to share any examples / links? Thanks.
如果是,是否可以分享任何示例/链接?谢谢。
Regards,V
问候,V
回答by strmqm
I agree with the other response that Spring JMS is simple (providing you know a bit of Spring, and have the framework) but it's quite straightforward from plain old Java too. Just write some code within main to setup a ConnectionFactory, Connection, and Session, e.g. see
我同意另一个回应,即 Spring JMS 很简单(前提是您对 Spring 有所了解,并拥有该框架),但它与普通的旧 Java 相比也非常简单。只需在 main 中编写一些代码来设置 ConnectionFactory、Connection 和 Session,例如参见
http://download.oracle.com/javaee/1.4/tutorial/doc/JMS4.html
http://download.oracle.com/javaee/1.4/tutorial/doc/JMS4.html
you can either look up a ConnectionFactory from JNDI or instantiate a provider-specific one yourself; e.g. for WebSphere MQ you would write something like:
您可以从 JNDI 查找 ConnectionFactory 或自己实例化一个特定于提供者的连接工厂;例如,对于 WebSphere MQ,您将编写如下内容:
MQConnectionFactory cf = new MQConnectionFactory();
cf.setHostName(HOSTNAME);
cf.setPort(PORT);
cf.setChannel(CHANNEL);
cf.setTransportType(WMQConstants.WMQ_CM_CLIENT);
(other providers are available, I just can't write the code off the top of my head :) then standard JMS
(其他提供程序可用,我只是无法编写代码:)然后是标准 JMS
Connection c = cf.createConnection();
Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue q = s.createQueue("myQueue");
MessageConsumer c = s.createConsumer(q);
at this point you have two options. You could create a javax.jms.MessageListener implementation and set it on the MessageConsumer, or if you want more direct control you can either kick off a Thread that uses the MessageConsumer to do either a get-with-wait (receive(int timeout)) or a get with no wait (receiveNoWait()) and then a sleep until the next receive. Don't use receive() though, it's never a good idea with JMS. If you want more than one polling thread then you should check the concurrency restrictions on JMS Session/Consumer objects.
此时你有两个选择。您可以创建一个 javax.jms.MessageListener 实现并将其设置在 MessageConsumer 上,或者如果您想要更直接的控制,您可以启动一个使用 MessageConsumer 的线程来执行 get-with-wait (receive(int timeout) ) 或无需等待的获取 (receiveNoWait()),然后休眠直到下一次接收。不过不要使用receive(),这对于JMS 来说从来都不是一个好主意。如果您想要多个轮询线程,那么您应该检查 JMS Session/Consumer 对象的并发限制。
Don't forget to call Connection.start() once the setup is done if you want anything to happen
如果您希望任何事情发生,请不要忘记在设置完成后调用 Connection.start()
Advantages are you only need your JMS provider .jars on the classpath, no framework etc. Disadvantages are that it's more complex than Spring, which provides quite an elegant solution to this - note how much setup code there is here, compared to Spring which abstracts it all out. Really depends on your preferences, other requirements etc.
优点是你只需要在类路径上的 JMS 提供者 .jars,没有框架等。缺点是它比 Spring 更复杂,它提供了一个非常优雅的解决方案 - 注意这里有多少设置代码,与抽象的 Spring 相比这一切都出来了。真的取决于您的喜好,其他要求等。
回答by Ryan Stewart
By far the simplest way I know to consume JMS messages is with Spring JMS. In particular, using the Spring JMS namespace, it can be as simple as:
到目前为止,我知道使用 JMS 消息的最简单方法是使用Spring JMS。特别是,使用Spring JMS 命名空间,它可以像这样简单:
<jms:listener-container connection-factory="aJmsConnectionFactory">
<jms:listener destination="someQueue" ref="theBeanToDelegateTo" method="theMethodToInvoke"/>
</jms:listener-container>
which will consume from "someQueue" and pass the messages to the bean named "theBeanToDelegateTo", method "theMethodToInvoke", converting messages to Java types as specified by SimpleMessageConverter.
它将从“someQueue”消费并将消息传递给名为“theBeanToDelegateTo”的 bean,方法“theMethodToInvoke”,将消息转换为SimpleMessageConverter指定的 Java 类型。
Edit:I've just created a sample project on github that does exactly what you're asking. Browse the source at https://github.com/zzantozz/testbed/tree/master/basic-spring-jmsor clone and run it:
编辑:我刚刚在 github 上创建了一个示例项目,它完全符合您的要求。在https://github.com/zzantozz/testbed/tree/master/basic-spring-jms浏览源代码或克隆并运行它:
git clone git://github.com/zzantozz/testbed.git tmp
cd tmp
mvn compile exec:java -Dexec.mainClass=rds.jms.Main -pl basic-spring-jms
It's a total of two classes--could have just been one, but two felt cleaner--and it handles starting and stopping both a JMS broker and a Spring container. There's a main class that starts things and waits for the user to shut it down. When Spring starts, it inits a bean that begins sending JMS messages. There's also a Spring message listener as I described above that consumes messages and passes them to that same bean, which prints them to stdout.
它总共有两个类——可能只是一个类,但感觉两个更干净——它处理启动和停止 JMS 代理和 Spring 容器。有一个主类可以启动并等待用户将其关闭。当 Spring 启动时,它会初始化一个开始发送 JMS 消息的 bean。还有一个我上面描述的 Spring 消息侦听器,它使用消息并将它们传递给同一个 bean,后者将它们打印到 stdout。