非阻塞 Jms 队列发送器

问题描述 投票:0回答:2

我想将日志/审核事件发布到 JMS 队列服务器。我实现了“QueueConnectActor”,它使用以下代码构造消息、创建队列并发送消息。

这会阻塞对 JMS 的调用。我想知道是否有更好的非阻塞方式将消息发送到队列?换句话说,playframework 上 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();

谢谢!

playframework playframework-2.0 jms nonblocking playframework-2.2
2个回答
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);

-1
投票

我目前正在研究任何非阻塞发送/接收 JMS 消息的方法。但找不到任何东西。看起来 JMS 是很久以前创建的,没有人费心为其提供非阻塞的东西。

© www.soinside.com 2019 - 2024. All rights reserved.