JMS点对点消息传递域示例

JMS消息传递域包括:点对点域和发布-订阅域

在点对点中,发送者将消息传递到队列,而单个接收者从队列中取出消息。
接收方在发送消息时不需要监听队列。

点对点消息传递发布者应用程序流程

  • 首先,我们将获得 JMS 服务器的初始上下文对象。
  • 之后使用初始上下文对象查找队列对象。
  • 我们将再次使用初始上下文对象来查找队列连接工厂。
  • 然后使用队列连接工厂创建队列连接,因为它代表 JMS 服务器的物理连接。
  • 创建队列连接工厂后,我们将创建队列会话,其中第一个参数将决定会话是否被处理。但我们将使用非事务性会话。
  • 在此之后为队列创建一个队列发送者,然后创建一条消息。
  • 然后将诸如“Hello World”之类的消息发送到队列对象。
  • 之后关闭队列连接,当我们关闭队列连接时,它将自动关闭会话和队列发送方。

示例:

package pointToPoint;                                                                      
import javax.naming.InitialContext;

import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.QueueSender;
import javax.jms.DeliveryMode;
import javax.jms.QueueSession;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;

public class Sender
{
    public static void main(String[] args) throws Exception
    {
       // get the initial context
       InitialContext context = new InitialContext();

       // lookup the queue object
       Queue queue = (Queue) context.lookup("queue/queue0");

       // lookup the queue connection factory
       QueueConnectionFactory conFactory = (QueueConnectionFactory) context.lookup ("queue/connectionFactory");

       // create a queue connection
       QueueConnection queConn = conFactory.createQueueConnection();

       // create a queue session
       QueueSession queSession = queConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);

       // create a queue sender
       QueueSender queSender = queSession.createSender(queue);
       queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

       // create a simple message to say "Hello World"
       TextMessage message = queSession.createTextMessage("Hello World");

       // send the message
       queSender.send(message);

       // print what we did
       System.out.println("Message Sent: " + message.getText());

       // close the queue connection
       queConn.close();
    }
}

点对点消息传送订户应用程序流程

接收方的大部分步骤与发送方应用程序相同,只是它将侦听消息而不是发送 JMS 消息。

package pointToPoint;
import javax.naming.InitialContext;                                                                        
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.QueueSession;
import javax.jms.QueueReceiver;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;

public class Receiver
{
    public static void main(String[] args) throws Exception
    {
       // get the initial context
       InitialContext context = new InitialContext();

      // lookup the queue object
       Queue queue = (Queue) context.lookup("queue/queue0");

       // lookup the queue connection factory
       QueueConnectionFactory conFactory = (QueueConnectionFactory) context.lookup ("queue/connectionFactory");

       // create a queue connection
       QueueConnection queConn = conFactory.createQueueConnection();

       // create a queue session
       QueueSession queSession = queConn.createQueueSession(false,   
       Session.AUTO_ACKNOWLEDGE);
       // create a queue receiver
       QueueReceiver queReceiver = queSession.createReceiver(queue);

       // start the connection
       queConn.start();

       // receive a message
       TextMessage message = (TextMessage) queueReceiver.receive();

       // print the message
       System.out.println("Message Received: " + message.getText());

       // close the queue connection
       queConn.close();
    }
}
日期:2020-09-17 00:09:50 来源:oir作者:oir