0

我正在使用 Websphere java 类在我的应用程序中实现 jms。

发件人代码:

        MQQueueConnectionFactory connectionFactory = new MQQueueConnectionFactory();
        connectionFactory.setHostName(environment.getProperty(MQ_CONNECTION_HOSTNAME));
        connectionFactory.setPort(Integer.parseInt(environment.getProperty(MQ_CONNECTION_PORT)));
        connectionFactory.setQueueManager(environment.getProperty(MQ_CONNECTION_QMANAGER));
        connectionFactory.setChannel(environment.getProperty(MQ_CONNECTION_CHANNEL));
        connectionFactory.setTransportType(1);
        final String username = environment.getProperty(MQ_CONNECTION_USERNAME);
        final String password = environment.getProperty(MQ_CONNECTION_PASSWORD);
        MQQueueConnection connection = null;
        if(username != null && username.trim().length() > 0 && password != null && password.trim().length() > 0) {
            connection = (MQQueueConnection) connectionFactory.createQueueConnection(username, password);
        }
        else {
            connection = (MQQueueConnection) connectionFactory.createQueueConnection();
        }
        senderSession = (MQQueueSession) connection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);            
        MQQueue queue = (MQQueue) senderSession.createQueue("queue:///" + environment.getProperty(MQ_CONNECTION_QUEUE));
        MQQueueSender sender =  (MQQueueSender) senderSession.createSender(queue);
        JMSMessage message = (JMSMessage)senderSession.createTextMessage(messageContent);
        connection.start();          
        sender.send(message);
        message.acknowledge()

收货人代码:

 final MQQueueConnection connection = (MQQueueConnection) (useAuth ? connectionFactory.createQueueConnection(username, password) :      connectionFactory.createQueueConnection());
    connection.start();
    final MQQueueSession receiverSession = (MQQueueSession) connection.createQueueSession(true, Session.CLIENT_ACKNOWLEDGE);
    final MQQueue queue = MQQueue)receiverSession.createQueue(queueName);
    MQQueueReceiver receiver = createReceiver(session, queue);
    receiver.setMessageListener(listener);

当我使用这些设置发送消息时,消息侦听器永远不会收到任何消息。但是,当我打开两个会话时,交易为假,一切似乎都正常。我可以理解原因。我想要交易会话。

如果需要任何其他详细信息,请告诉我

4

2 回答 2

1

如果您想使用本地事务(同步点),那么在您发送消息后,您需要提交它。IE

senderSession.commit();
于 2013-07-03T17:57:27.437 回答
1

不需要来自发送方的 message.Acknowledge()。它只能在接收方和具有确认类型 ClientAcknowledge 的会话中使用。此调用将通知消息传递提供程序(在本例中为 MQ)从队列中删除当前和之前收到的所有消息。

于 2013-07-04T08:08:15.133 回答