在客户端使用TemporaryQueue与JBoss服务器bean同步请求/回复JMS

时间:2014-06-11 16:32:46

标签: jboss jms hornetq

我在JBoss 7.1上运行了一个MDB,在另一台机器上运行了一个简单的Java应用程序作为客户端。目标如下:

  1. 客户端向服务器发送请求(ObjectMessage)
  2. 服务器处理请求并将响应发送回客户端(再次返回ObjectMessage)
  3. 我想在客户端上使用TemporaryQueue来监听响应(因为我不知道如何异步执行),并且JMSReplyTo Message的属性能够正确回复,因为我应该支持多个独立的客户端。

    这是客户:

    public class MessagingService{
    
    private static final String JBOSS_HOST = "localhost";
    private static final int JBOSS_PORT = 5455;
    private static Map connectionParams = new HashMap();
    
    private Window window;
    
    private Queue remoteQueue;
    private TemporaryQueue localQueue;
    
    private ConnectionFactory connectionFactory;
    private Connection connection;
    private Session session;
    
    
    
    public MessagingService(Window myWindow){
        this.window = myWindow;
        MessagingService.connectionParams.put(TransportConstants.PORT_PROP_NAME, JBOSS_PORT);
        MessagingService.connectionParams.put(TransportConstants.HOST_PROP_NAME, JBOSS_HOST);
        TransportConfiguration transportConfiguration =  new TransportConfiguration(NettyConnectorFactory.class.getName(), connectionParams);  
        this.connectionFactory = (ConnectionFactory) HornetQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, transportConfiguration);
    }
    
    
    public void sendRequest(ClientRequest request) {
        try {
            connection = connectionFactory.createConnection();
    
            this.session = connection.createSession(false, QueueSession.AUTO_ACKNOWLEDGE);
    
            this.remoteQueue = HornetQJMSClient.createQueue("testQueue");
            this.localQueue = session.createTemporaryQueue();
    
            MessageProducer producer = session.createProducer(remoteQueue);
            MessageConsumer consumer = session.createConsumer(localQueue);
    
            ObjectMessage message = session.createObjectMessage();
            message.setObject(request);
            message.setJMSReplyTo(localQueue);
            producer.send(message);
    
            ObjectMessage response = (ObjectMessage) consumer.receive();
            ServerResponse serverResponse = (ServerResponse) response.getObject();
            this.window.dispatchResponse(serverResponse);
    
            this.session.close();
    
        } catch (JMSException e) {
            // TODO splittare e differenziare
            e.printStackTrace();
        }
    }
    

    现在我在编写服务器端时遇到了麻烦,因为我无法弄清楚如何建立与TemporaryQueue的连接......

        public void onMessage(Message message) {
        try {
            if (message instanceof ObjectMessage) {
                Destination replyDestination = message.getJMSReplyTo();
                ObjectMessage objectMessage = (ObjectMessage) message;
                ClientRequest request = (ClientRequest) objectMessage.getObject();
    
                System.out.println("Queue: I received an ObjectMessage at " + new Date());
                System.out.println("Client Request Details: ");
                System.out.println(request.getDeparture());
                System.out.println(request.getArrival());
                System.out.println(request.getDate());
                System.out.println("Replying...");
    
                                // no idea what to do here
                                Connection connection = ? ? ? ? ? ? ? ? 
    
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                MessageProducer replyProducer = session.createProducer(replyDestination);
                ServerResponse serverResponse = new ServerResponse("TEST RESPONSE");
                ObjectMessage response = session.createObjectMessage();
                response.setObject(serverResponse);
                replyProducer.send(response);
    
            } else {
                System.out.println("Not a valid message for this Queue MDB");
            }
    
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
    

    我无法弄清楚我错过了什么

1 个答案:

答案 0 :(得分:0)

你在这里提出了错误的问题。你应该看看如何在任何Bean中创建一个连接。

您需要获取ConnectionFactory,并相应地创建连接。

有关更多信息,请查看HornetQ下载中的javaee示例。

具体看一下javaee / mdb -tx-send /当你下载hornetq。

@MessageDriven(name = "MDBMessageSendTxExample",
               activationConfig =
                     {
                        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
                        @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/testQueue")
                     })
public class MDBMessageSendTxExample implements MessageListener
{
   @Resource(mappedName = "java:/JmsXA")
   ConnectionFactory connectionFactory;

   public void onMessage(Message message)
   {
     Connection conn = null;
     try
     {

        // your code here...

        //Step 11. we create a JMS connection
        conn = connectionFactory.createConnection();

        //Step 12. We create a JMS session
        Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

        //Step 13. we create a producer for the reply queue
        MessageProducer producer = sess.createProducer(replyDestination);

        //Step 14. we create a message and send it
        producer.send(sess.createTextMessage("this is a reply"));

     }
     catch (Exception e)
     {
        e.printStackTrace();
     }
     finally
     {
        if(conn != null)
        {
           try
           {
              conn.close();
           }
           catch (JMSException e)
           {
           }
        }
     }
  }