Java 客户端监听 WebSphere MQ Server?
我需要编写一个侦听 WebSphere MQ Server 的 Java 客户端。消息被放入服务器的队列中。
我开发了这段代码,但不确定它是否正确。如果正确的话我该如何测试呢?
这是一个独立的 Java 项目,没有应用程序服务器支持。我应该将哪些 jar 放入类路径中?
我有 MQ 设置,我应该将其放入代码中吗?标准JMS可以跳过这些设置吗?令人困惑....
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class Main {
Context jndiContext = null;
QueueConnectionFactory queueConnectionFactory = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue controlQueue = null;
QueueReceiver queueReceiver = null;
private String queueSubject = "";
private void start() {
try {
queueConnection.start();
queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = queueSession.createQueue(queueSubject);
MessageConsumer consumer = queueSession.createConsumer(destination);
consumer.setMessageListener(new MyListener());
} catch (Exception e) {
e.printStackTrace();
}
}
private void close() {
try {
queueSession.close();
queueConnection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void init() {
try {
jndiContext = new InitialContext();
queueConnectionFactory = (QueueConnectionFactory) this.jndiLookup("QueueConnectionFactory");
queueConnection = queueConnectionFactory.createQueueConnection();
queueConnection.start();
} catch (Exception e) {
System.err.println("Could not create JNDI API " + "context: " + e.toString());
System.exit(1);
}
}
private class MyListener implements MessageListener {
@Override
public void onMessage(Message message) {
System.out.println("get message:" + message);
}
}
private Object jndiLookup(String name) throws NamingException {
Object obj = null;
if (jndiContext == null) {
try {
jndiContext = new InitialContext();
} catch (NamingException e) {
System.err.println("Could not create JNDI API " + "context: " + e.toString());
throw e;
}
}
try {
obj = jndiContext.lookup(name);
} catch (NamingException e) {
System.err.println("JNDI API lookup failed: " + e.toString());
throw e;
}
return obj;
}
public Main() {
}
public static void main(String[] args) {
new Main();
}
}
MQ队列设置
<queue-manager>
<name>AAA</name>
<port>1423</port>
<hostname>ddd</hostname>
<clientChannel>EEE.CLIENTS.00</clientChannel>
<securityClass>PKIJCExit</securityClass>
<transportType>1</transportType>
<targetClientMatching>1</targetClientMatching>
</queue-manager>
<queues>
<queue-details id="queue-1">
<name>GGGG.NY.00</name>
<transacted>false</transacted>
<acknowledgeMode>1</acknowledgeMode>
<targetClient>1</targetClient>
</queue-details>
</queues>
I need to write a Java client listening to WebSphere MQ Server. Message is put into a queue in the server.
I developed this code, but am not sure it is correct or not. If correct, then how can I test it?
This is a standalone Java project, no application server support. Which jars I should put into classpath?
I have the MQ settings, where I should put into my codes? Standard JMS can skip these settings? confusing ....
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class Main {
Context jndiContext = null;
QueueConnectionFactory queueConnectionFactory = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue controlQueue = null;
QueueReceiver queueReceiver = null;
private String queueSubject = "";
private void start() {
try {
queueConnection.start();
queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = queueSession.createQueue(queueSubject);
MessageConsumer consumer = queueSession.createConsumer(destination);
consumer.setMessageListener(new MyListener());
} catch (Exception e) {
e.printStackTrace();
}
}
private void close() {
try {
queueSession.close();
queueConnection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void init() {
try {
jndiContext = new InitialContext();
queueConnectionFactory = (QueueConnectionFactory) this.jndiLookup("QueueConnectionFactory");
queueConnection = queueConnectionFactory.createQueueConnection();
queueConnection.start();
} catch (Exception e) {
System.err.println("Could not create JNDI API " + "context: " + e.toString());
System.exit(1);
}
}
private class MyListener implements MessageListener {
@Override
public void onMessage(Message message) {
System.out.println("get message:" + message);
}
}
private Object jndiLookup(String name) throws NamingException {
Object obj = null;
if (jndiContext == null) {
try {
jndiContext = new InitialContext();
} catch (NamingException e) {
System.err.println("Could not create JNDI API " + "context: " + e.toString());
throw e;
}
}
try {
obj = jndiContext.lookup(name);
} catch (NamingException e) {
System.err.println("JNDI API lookup failed: " + e.toString());
throw e;
}
return obj;
}
public Main() {
}
public static void main(String[] args) {
new Main();
}
}
MQ Queue setting
<queue-manager>
<name>AAA</name>
<port>1423</port>
<hostname>ddd</hostname>
<clientChannel>EEE.CLIENTS.00</clientChannel>
<securityClass>PKIJCExit</securityClass>
<transportType>1</transportType>
<targetClientMatching>1</targetClientMatching>
</queue-manager>
<queues>
<queue-details id="queue-1">
<name>GGGG.NY.00</name>
<transacted>false</transacted>
<acknowledgeMode>1</acknowledgeMode>
<targetClient>1</targetClient>
</queue-details>
</queues>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一篇文章包含示例代码 在 WebSphere 上运行独立的 Java 应用程序MQ V6.0 可引导您解决大部分问题,包括如何使用免费的 WMQ 试用安装进行测试。与 v7 或 v7.1 的主要区别(如评论中所述)是,如果您想使用主题,则无需启动代理组件。除此之外,本文应该可以在当前的 WMQ 客户端和/或服务器上正常工作。
此外,请参阅WebSphere MQ 使用 Java 手册(v7.0 客户端)或 使用适用于 Java 的 WebSphere MQ 类手册(v7.1客户端)为您的客户端提供适当的
CLASSPATH
和其他设置。请记住使用适合您的客户端版本而不是服务器版本的信息中心。您可以混合搭配客户端和服务器版本,但您只能获得服务器支持的功能。例如,将 WMQ v7.1 客户端与 WMQ v7.0 服务器一起使用是完全有效的。最后,免费客户端下载提供了许多示例程序,它们完全符合您所描述的功能。有些使用 JNDI 来查找 WMQ 资源,其他则使用 Java 方法并且可以使用标准 Java 属性文件进行填充。带有
-nojndi
选项的内容展示了如何在运行时在代码中初始化 WMQ 对象。这些位于最新 Windows 客户端安装 (SupportPac MQC71) 下。您还可以使用 v7.0 客户端 (SupportPac MQC7)。我建议使用示例来开始,而不是从头开始。为什么要重新发明轮子,对吧?
除了许多示例程序之外,供应商安装还包括所有必需的 jar 文件。请注意,
CLASSPATH
中的内容因 WMQ 客户端版本而异,因此请参阅信息中心。更高版本要简单得多,只需要 CLASSPATH 中的几个 jar 文件。如果您想要下载 WMQ 试用版进行测试并且没有管理员权限在 Windows 工作站上,您可以轻松地将其安装在 RedHat 或 SUSE 虚拟机上。通过一些操作,您还可以轻松地在 Ubuntu 上安装,如 Andy Piper 博客文章。
There is an article with sample code Running a standalone Java application on WebSphere MQ V6.0 which walks you through most of your questions, including how you might test with a free WMQ trial install. The main difference (as discussed in the comments) to v7 or v7.1 is that there is no broker component to start if you want to use topics. Other than that, the article should work fine with the current WMQ client and/or server.
In addition, please refer to the WebSphere MQ Using Java manual (v7.0 client) or the Using WebSphere MQ Classes for Java manual (v7.1 client) for the appropriate
CLASSPATH
and other settings for your client. Remember to use the Infocenter appropriate to your client version and not to the server version. You can mix and match client and server version but you get only the features supported by the server. For example, using the WMQ v7.1 client with the WMQ v7.0 server is perfectly valid.Finally, there are a number of sample programs supplied with the free client download that do exactly what you are describing. Some use JNDI to lookup the WMQ resources, others use Java methods and can be populated with standard Java property files. The ones with a
-nojndi
option show how to initialize your WMQ objects in the code at run time. These are under...in the latest Windows client install (SupportPac MQC71). You can also use the v7.0 client (SupportPac MQC7). I would recommend using the samples to get started rather than starting from scratch. Why reinvent the wheel, right?
In addition to the many sample programs, the vendor install includes all of the requisite jar files. Note that what goes in the
CLASSPATH
changes by WMQ client version so refer to the Infocenter. The later versions are much simpler and require only a couple of jar files in theCLASSPATH
.If you want to download the WMQ trial for testing and do not have Administrator rights on your Windows workstation, you can install it easily on a RedHat or SUSE virtual machine. With a bit of massaging you can also easily install on Ubuntu as described in an Andy Piper blog post.
如果您可以选择,我建议您引入 Spring 框架来处理 JMS 通信。这样,您只需要编写业务逻辑,并将错误处理留给 Spring。
Spring JMS
下载最新的Spring JARS 并查看配置 DefaultMessageListenerContainer 为您应用。然后,您将使用
onMessage()
事件编写自己的 POJO(普通旧 Java 对象),每次新消息到达时都会调用该事件。我发现本教程,您可以找到有用的开始
If you have the option, I would recommend that you introduce the Spring Framework to handle the JMS communication. That way you only need to write your business logic and can leave the error handling up to Spring.
Spring JMS
Download the latest Spring JARS and look at configuring a DefaultMessageListenerContainer for your application. You will then write your own POJO (plain old java object) with an
onMessage()
event that gets called everytime a new message arrives.I found this tutorial that you may find useful to start with
我用 JavaFx2 为 Windows 和 Mac osx 开发了一个小型客户端应用程序。它可以在 Source Forge (https://sourceforge.net/projects/mqconsole) 上找到。
您可以看到监听队列中新消息的示例。
该程序列出了队列、每个队列中的消息、查看消息的详细信息以及向队列发送消息并监听响应。
您可以查看代码、使用它并改进它。
There is a small client application that I developed in JavaFx2 for windows and Mac osx. It's available on source forge (https://sourceforge.net/projects/mqconsole).
You can see an example of listening to a queue for new messages.
The program lists the queues, the messages in each queue, view the details of the message and send a message to a queue and listen to the response.
You can check out the code, use it and improve it.