请帮助 MQ nubee 编写他的第一个 Java 客户端,我在 Oracle 文档中有点迷失了。我已经启动并运行了 OpenMQ。在 OpenMQ 管理控制台中,我建立了一个名为“MyFirstTest”的代理,6 个服务中有 1 个是“jms”(这似乎是最容易使用的服务),该服务也已启动并正在运行(例如:服务状态正在运行)。所以我来到了有趣的部分。我如何连接到代理“MyFirstTest”,然后发送一条消息,最后但至少从第二个客户端读取这条消息。
我想我必须找到已经存在的队列而不是使用新的 com.sun.messaging.Queue
任何示例或链接表示赞赏。
public class HelloWorldMessage {
public static void main(String[] args) {
try {
ConnectionFactory myConnFactory;
Queue myQueue;
myConnFactory = new com.sun.messaging.ConnectionFactory();
Connection myConn = myConnFactory.createConnection();
Session mySess = myConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
myQueue = new com.sun.messaging.Queue("MyFirstTest");
//Create a message producer.
MessageProducer myMsgProducer = mySess.createProducer(myQueue);
//Create and send a message to the queue.
TextMessage myTextMsg = mySess.createTextMessage();
myTextMsg.setText("Hello World");
System.out.println("Sending Message: " + myTextMsg.getText());
myMsgProducer.send(myTextMsg);
//Create a message consumer.
MessageConsumer myMsgConsumer = mySess.createConsumer(myQueue);
//Start the Connection created in step 3.
myConn.start();
//Receive a message from the queue.
Message msg = myMsgConsumer.receive();
//Retreive the contents of the message.
if (msg instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) msg;
System.out.println("Read Message: " + txtMsg.getText());
}
//Close the session and connection resources.
mySess.close();
myConn.close();
} catch (Exception jmse) {
System.out.println("Exception occurred : " + jmse.toString());
jmse.printStackTrace();
}
}
}