JMS消息传递模式有点对点(P2P)模式和发布/订阅(Publisher/Subscriber)模式。点对点模式使用队列,发布/订阅模式使用Topic。
一、P2P模式的消息发送
QueueConnectionFactory qConnectionFactory = (QueueConnectionFactory)ctx.lookup( " jmsfactory " );
QueueConnection qConnection = qConnectionFactory.createQueueConnection();
// QueueConnection.createQueueConnection的第一个参数表示是否使用事务,如果是true,则要调用QueueSession.commit()进行提交,调用QueueSession.rollback()进行回滚。
QueueSession qSession = qConnection.createQueueSession( false ,javax.jms.QueueSession.AUTO_ACKNOWLEDGE);
Queue q = (Queue)ctx.lookup( " jmsqueue " );
QueueSender qSender = qSession.createSender(q);
Message textMsg = qSession.createTextMessage();
qSender.send(textMsg);
二、P2P模式的消息接收
1 、同步
使用QueueReceiver.receive()
使用QueueReceiver.receiverNoWait()
代码示例:
q = (Queue)ctx.lookup( " jmsqueue " );
qReceiver = qSession.createReceiver(q);
qConnection.start();
textMsg = (TextMessage)qReceiver.receiveNoWait();
while (textMsg != null ) ... {
System.out.println("Receiving message " + textMsg.getText());
textMsg = (TextMessage)qReceiver.receiveNoWait();
}
qConnection.stop();
2 、异步
编写Message Listener(实现javax.jms.MessageListener接口的类),当消息到来时,系统将自动调用Message Listener对象的onMessage()方法
调用QueueReceiver.setMessageListener(Message Listener对象)进行注册。
代码示例:
public class AsyncQueueReceiver implements MessageListener ... {
public void onMessage(Message message)...{
if(message instanceof TextMessage)...{
try...{
TextMessage textMsg = message;
//对消息进行处理
String msg = textMsg.getText();
System.out.println("Received message:" + msg);
}
}
}
}
三、发布 / 订阅模式的消息发送
代码示例:
TopicConnectionFactory tConnectionFactory = (TopicConnectionFactory)ctx.lookup( " jmsfactory " );
TopicConnection tConnection = tConnectionFactory.createTopicConnection();
TopicSession tSession = tConnection.createTopicSession( false ,javax.jms.TopicSession.AUTO_ACKNOWLEDGE);
Topic t = (Topic)ctx.lookup( " jmstopic " );
TopicPublisher tPublisher = tSession.createPublisher(t);
Message textMsg = tSession.createTextMessage();
// TODO:为textMsg设置消息内容
// 发布消息
tPublisher.publish(textMsg);
二、发布 / 订阅模式的消息接收
1 、同步接收代码示例:
TopicSubscriber tSubscriber = tSession.createSubscriber(t);
tConnection.start();
textMsg = (TextMessage)tSubscriber.receive();
while ( true ) ... {
msg = textMsg.getText();
System.out.println("Receiving message: " + msg);
...
}
2 、异步接收代码示例:
代码示例:
public class AsyncTopicSubscriber implements MessageListener ... {
public void onMessage(Message message)...{
if(message instanceof TextMessage)...{
try...{
TextMessage textMsg = message;
//对消息进行处理
String msg = textMsg.getText();
System.out.println("Received message:" + msg);
}
}
}
}
QueueConnectionFactory qConnectionFactory = (QueueConnectionFactory)ctx.lookup( " jmsfactory " );
QueueConnection qConnection = qConnectionFactory.createQueueConnection();
// QueueConnection.createQueueConnection的第一个参数表示是否使用事务,如果是true,则要调用QueueSession.commit()进行提交,调用QueueSession.rollback()进行回滚。
QueueSession qSession = qConnection.createQueueSession( false ,javax.jms.QueueSession.AUTO_ACKNOWLEDGE);
Queue q = (Queue)ctx.lookup( " jmsqueue " );
QueueSender qSender = qSession.createSender(q);
Message textMsg = qSession.createTextMessage();
qSender.send(textMsg);
二、P2P模式的消息接收
1 、同步
使用QueueReceiver.receive()
使用QueueReceiver.receiverNoWait()
代码示例:
q = (Queue)ctx.lookup( " jmsqueue " );
qReceiver = qSession.createReceiver(q);
qConnection.start();
textMsg = (TextMessage)qReceiver.receiveNoWait();
while (textMsg != null ) ... {
System.out.println("Receiving message " + textMsg.getText());
textMsg = (TextMessage)qReceiver.receiveNoWait();
}
qConnection.stop();
2 、异步
编写Message Listener(实现javax.jms.MessageListener接口的类),当消息到来时,系统将自动调用Message Listener对象的onMessage()方法
调用QueueReceiver.setMessageListener(Message Listener对象)进行注册。
代码示例:
public class AsyncQueueReceiver implements MessageListener ... {
public void onMessage(Message message)...{
if(message instanceof TextMessage)...{
try...{
TextMessage textMsg = message;
//对消息进行处理
String msg = textMsg.getText();
System.out.println("Received message:" + msg);
}
}
}
}
三、发布 / 订阅模式的消息发送
代码示例:
TopicConnectionFactory tConnectionFactory = (TopicConnectionFactory)ctx.lookup( " jmsfactory " );
TopicConnection tConnection = tConnectionFactory.createTopicConnection();
TopicSession tSession = tConnection.createTopicSession( false ,javax.jms.TopicSession.AUTO_ACKNOWLEDGE);
Topic t = (Topic)ctx.lookup( " jmstopic " );
TopicPublisher tPublisher = tSession.createPublisher(t);
Message textMsg = tSession.createTextMessage();
// TODO:为textMsg设置消息内容
// 发布消息
tPublisher.publish(textMsg);
二、发布 / 订阅模式的消息接收
1 、同步接收代码示例:
TopicSubscriber tSubscriber = tSession.createSubscriber(t);
tConnection.start();
textMsg = (TextMessage)tSubscriber.receive();
while ( true ) ... {
msg = textMsg.getText();
System.out.println("Receiving message: " + msg);
...
}
2 、异步接收代码示例:
代码示例:
public class AsyncTopicSubscriber implements MessageListener ... {
public void onMessage(Message message)...{
if(message instanceof TextMessage)...{
try...{
TextMessage textMsg = message;
//对消息进行处理
String msg = textMsg.getText();
System.out.println("Received message:" + msg);
}
}
}
}