JMS 客户端独立 Java 程序
我想使用独立的 java 程序来轮询和检索 JMS 队列中的消息,而不是使用 MDB。是否可以?
如果是,是否可以分享任何示例/链接?谢谢。
问候,V
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?
If it is, would it be possible to share any examples / links? Thanks.
Regards,V
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我同意另一个回答,即 Spring JMS 很简单(只要您了解一点 Spring,并且有框架),但它与普通的旧 Java 相比也非常简单。只需在 main 中编写一些代码即可设置 ConnectionFactory、Connection 和 Session,例如,请参阅
http://download.oracle.com/javaee/1.4/tutorial/doc/JMS4.html
您可以从 JNDI 查找 ConnectionFactory 或自己实例化特定于提供者的 ConnectionFactory;例如,对于WebSphere MQ,您可以编写如下内容:(
其他提供程序也可用,我只是无法随心所欲地编写代码:) 那么标准JMS
此时您有两个选择。您可以创建一个 javax.jms.MessageListener 实现并将其设置在 MessageConsumer 上,或者如果您想要更直接的控制,您可以启动一个使用 MessageConsumer 的线程来执行 get-with-wait (receive(int timeout) )或不等待的获取(receiveNoWait()),然后休眠直到下一次接收。但不要使用 receive(),这对于 JMS 来说绝不是一个好主意。如果您需要多个轮询线程,那么您应该检查 JMS Session/Consumer 对象的并发限制。
如果您希望发生任何事情,请不要忘记在设置完成后调用 Connection.start()
优点是您只需要类路径上的 JMS 提供程序 .jar,不需要框架等。缺点是它比 Spring 更复杂,Spring 比 Spring 更复杂。对此提供了一个相当优雅的解决方案 - 请注意这里有多少设置代码,而 Spring 则将其全部抽象出来。实际上取决于您的喜好、其他要求等。
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
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:
(other providers are available, I just can't write the code off the top of my head :) then standard JMS
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.
Don't forget to call Connection.start() once the setup is done if you want anything to happen
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 消息的最简单方法是使用 Spring JMS。特别是,使用 Spring JMS命名空间,它可以像这样简单:
将从“someQueue”消费并将消息传递给名为“theBeanToDelegateTo”的bean,方法“theMethodToInvoke”,将消息转换为指定的Java类型SimpleMessageConverter。
编辑:我刚刚在 github 上创建了一个示例项目,它完全符合您的要求。浏览源代码 https://github.com/zzantozz/testbed/tree /master/basic-spring-jms 或克隆并运行它:
它总共有两个类——本来可以只有一个,但两个感觉更干净——它处理启动和停止 JMS 代理和弹簧容器。有一个主类启动事物并等待用户将其关闭。当 Spring 启动时,它会初始化一个开始发送 JMS 消息的 bean。正如我上面所描述的,还有一个 Spring 消息监听器,它使用消息并将它们传递给同一个 bean,该 bean 将它们打印到 stdout。
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:
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.
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-jms or clone and run it:
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.