jms 发送接收讯息
jms 发送接收消息jms 发送接收消息????jms发送和接收消息的步骤1.建立连接ActiveMQConnectionFactory ac
jms 发送接收消息
jms 发送接收消息????jms发送和接收消息的步骤1.建立连接ActiveMQConnectionFactory ac = new ActiveMQConnectionFactory();// ac.setBrokerURL("tcp://localhost:61616?trace=false");?ConnectionFactory connectionFactory = ac;?2.获取session,Connection connection = connectionFactory.createConnection();connection.start();?Session session = connection.createSession(Boolean.TRUE,Session.CLIENT_ACKNOWLEDGE);?3. 建立管道Destination destination = session.createQueue("my-queue");note:建立了一个名字叫"my-queue"的queue的管道?4.创建消息和消息头MessageProducer producer = session.createProducer(destination);?// 不要求JMS provider持久保存消息。producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);?// 指示JMS provider持久保存消息,以保证消息不会因为JMS provider的失败而丢失// producer.setDeliveryMode(DeliveryMode.PERSISTENT);?// 设置优先级producer.setPriority(7);?// 设置是否记录消息被传递给send方法的时间,默认为记录producer.setDisableMessageTimestamp(false);?System.out.println("connection");// 发送消息?5.发送消息// 创建一条文本消息TextMessage textMessage = session.createTextMessage(message);producer.send(textMessage);?// 创建一条map消息MapMessage mapMessage = session.createMapMessage();mapMessage.setString("name", "丽仪");mapMessage.setString("sex", "女");producer.send(mapMessage);?// 创建一条对象消息ObjectMessage objMessage = session.createObjectMessage();objMessage.setObjectProperty("school", "广大");?// 通过消息生产者发出消息producer.send(objMessage); ??6.接收消息while (true) {Message message = consumer.receive(1000);?if (null != message) {//区分不同的消息类型if (message instanceof TextMessage)System.out.println("收到TextMessage消息:"+ ((TextMessage) message).getText());?if (message instanceof MapMessage)System.out.println("收到MapMessage消息:"+ ((MapMessage) message).getObject("name"));?if (message instanceof ObjectMessage)System.out.println("收到ObjectMessage消息:"+ ((ObjectMessage) message).getStringProperty("school"));?} elsebreak;}}?? ? ? ? ??7.关闭连接session.commit();session.close();connection.close();