我正在使用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);
当我使用这些设置发送消息时,消息侦听器从不接收任何消息。但当我打开两个transactiond为false的会话时,一切似乎都很好。我能理解原因。我想进行事务会话。
如果需要任何其他详细信息,请告诉我。
如果您想使用本地事务(syncpoint),那么在发送消息后,您需要提交它。即
senderSession.commit();
消息。不需要来自发送方的Acknowledge()。它只能在接收方和具有确认类型ClientAcknowledge的会话中使用。此调用将通知消息传递提供程序(在本例中为MQ)从队列中删除当前和以前接收到的所有消息。