我想将日志/审核事件发布到JMS队列服务器。我实现了“QueueConnectActor”,它使用以下代码构造消息,创建队列和发送消息。
这会阻止对JMS的调用。我想知道是否有更好的非bloking方式将消息发送到队列?换句话说,playframe上的jms客户端的任何引用/指针或示例代码。
QueueConnectionFactory factory = new com.tibco.tibjms.TibjmsQueueConnectionFactory(serverUrl);
QueueConnection connection = factory.createQueueConnection(userName,password);
QueueSession session = connection.createQueueSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);
javax.jms.Queue queue = session.createQueue(queueName);
QueueSender sender = session.createSender(queue);
javax.jms.TextMessage textMessage = session.createTextMessage();
textMessage.setText(eventXml);
sender.send(textMessage);
connection.close();
谢谢!
答案 0 :(得分:2)
JMS 1.1规范未指定非阻塞发送调用的API或选项。因此,JMS实现将不具有非阻塞即异步发送功能。但是,像WebSphere MQ这样的JMS实现具有提供者特定选项,可以使用非阻塞发送调用来发送消息。 (见下面的示例代码)
最近(大约一年前),JMS 2.0规范添加了一种新方法,允许应用程序异步发送消息。
下面的示例代码演示了使用WebSphere MQ的异步发送。
connection = cf.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("queue:///Q1");
// Request asynchronous sends to this destination. Note: Message will be
// sent asynchronously if the environment permits; otherwise, message will
// be sent synchronously. See documentation for further details.
((JmsDestination) destination).setIntProperty(WMQConstants.WMQ_PUT_ASYNC_ALLOWED,
WMQConstants.WMQ_PUT_ASYNC_ALLOWED_ENABLED);
producer = session.createProducer(destination);
long uniqueNumber = System.currentTimeMillis() % 1000;
TextMessage message = session
.createTextMessage("SimpleAsyncPutPTP: Your lucky number today is " + uniqueNumber);
// Start the connection
connection.start();
// And, send the message
producer.send(message);
System.out.println("Sent message:\n" + message);