SERVER端(相当于消费者):
@MessageDriven(
activationConfig ={
// javax.jms.Queue (p2p) 或者 javax.jms.Topic
@ActivationConfigProperty(propertyName = "destinationType" , propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination" , propertyValue = "queue/myqueue")
}
)
public class MyQueueMDBBean implements MessageListener {
public void onMessage(Message msg) {
TextMessage tmx = (TextMessage) msg;
System.out.println(tmx.toString());
}
}
CLIENT端(相当于发布者):
public class Main {
public static void main(String[] args) throws NamingException, JMSException {
InitialContext context = new InitialContext();
// 获得Destination对象 如果要用Topic把所有的Queue替换成Topic
Queue queue = (Queue)context.lookup("queue/myqueue");
// 获得QueueConnectionFactory对象
QueueConnectionFactory factory = (QueueConnectionFactory) context.lookup("ConnectionFactory");
// 创建QueueConnection对象
QueueConnection connection = factory.createQueueConnection();
// 创建QueueSession对象(agrs1:是否需要事物,agrs2:模型)
QueueSession session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
// 创建TextMessage并赋值
TextMessage tms = session.createTextMessage();
tms.setText("hello world!");
// 创建发送者,指定发送对象。TopicPublisher
QueueSender qSender = session.createSender(queue);
//发送
qSender.send(tms);
}
}