ActiveMQ(三)整合spring

配置





  
  
      
  

  
  

  
  
  
      
      
  


  
  
  
      
      
      
  

  
  
      
      
      
  

  

  
  
      
  



  
  
      
      
  

  
  
      
      
  
  

生产者 topic

@Component("topicSender")
public class TopicSender {

    @Autowired
    @Qualifier("jmsTopicTemplate")
    private JmsTemplate jmsTemplate;

    public void send(String queueName, final String message) {
        jmsTemplate.send(queueName, new MessageCreator() {

            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage(message);
                return textMessage;
            }
        });
    }
}

生产者 queue

@Component("queueSender")
public class QueueSender {

    @Autowired
    @Qualifier("jmsQueueTemplate")
    private JmsTemplate jmsTemplate;
    @Autowired
    private GetResponse getResponse;

    //json
    public void send(String queueName, final String message) {
        jmsTemplate.send(queueName, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                Message msg = session.createTextMessage(message);
                //配置,告诉消费者如何应答
                Destination tempDst = session.createTemporaryQueue();
                MessageConsumer responseConsumer = session.createConsumer(tempDst);
                responseConsumer.setMessageListener(getResponse);
                msg.setJMSReplyTo(tempDst);

                String uid = System.currentTimeMillis()+"";
                msg.setJMSCorrelationID(uid);

                return msg;
            }
        });

        //发送MapMessage
/*        jmsTemplate.send(queueName, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                MapMessage map = session.createMapMessage();
                map.setString("id", "10000");
                map.setString("name", "享学学员");
                return map;
            }
        });*/

        //发送ObjectMessage,被发送的实体类必须实现Serializable 接口
/*        jmsTemplate.send(queueName, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                User user = new User(10000,"享学学员");
                ObjectMessage objectMessage
                 = session.createObjectMessage(user);
                return objectMessage;
            }
        });*/


        //发送BytesMessage
        //protobuf,kyro,messgepack
/*        jmsTemplate.send(queueName, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                BytesMessage bytesMessage = session.createBytesMessage();
                bytesMessage.writeBytes("BytesMessage类型消息".getBytes());
                return bytesMessage;
            }
        });*/


        //发送StreamMessage
/*        jmsTemplate.send(queueName, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                StreamMessage streamMessage = session.createStreamMessage();
                streamMessage.writeString("享学学员");
                streamMessage.writeInt(10000);
                //streamMessage.writeString(age);
                return streamMessage;
            }
        });*/
    }


}

接受应答

@Component
public class GetResponse implements MessageListener {
    public void onMessage(Message message) {
        String textMsg = null;
        try {
            textMsg = ((TextMessage) message).getText();
            System.out.println("GetResponse accept msg : " + textMsg);
        } catch (JMSException e) {
            e.printStackTrace();
        }

    }
}

消费者Queue应答

@Component
public class QueueReceiver1 implements MessageListener {

    @Autowired
    private ReplyTo replyTo;

    public void onMessage(Message message) {
        try {
            String textMsg = ((TextMessage) message).getText();
            System.out.println("QueueReceiver1 accept msg : " + textMsg);
            // do business work;
            replyTo.send(textMsg,message);
        } catch (JMSException e) {
            e.printStackTrace();
        }

    }
}
@Component
public class QueueReceiver2 implements MessageListener {

    public void onMessage(Message message) {
        try {
            // 接收Text消息
            if (message instanceof TextMessage) {
                String textMsg = ((TextMessage) message).getText();
                System.out.println("QueueReceiver2 accept msg : " + textMsg);
            }

            // 接收Map消息
            if (message instanceof MapMessage) {
                MapMessage mm = (MapMessage) message;
                System.out.println("获取 MapMessage:   name:" + mm.getString("name")
                        + "     msg:" + mm.getString("msg"));
            }

/*          // 接收Object消息
            if (message instanceof ObjectMessage) {
                ObjectMessage objectMessage = (ObjectMessage) message;
                User user = (User) objectMessage.getObject();
                System.out.println("获取 ObjectMessage:  "+user);
            }*/

            // 接收bytes消息
/*          if (message instanceof BytesMessage) {
                byte[] b = new byte[1024];
                int len = -1;
                BytesMessage bm = (BytesMessage) message;
                while ((len = bm.readBytes(b)) != -1) {
                    System.out.println(new String(b, 0, len));
                }
            }*/

/*          // 接收Stream消息
            if (message instanceof StreamMessage) {
                StreamMessage streamMessage = (StreamMessage) message;
                System.out.println(streamMessage.readString());
                System.out.println(streamMessage.readInt());
            }*/
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

}
@Component
public class ReplyTo {

    @Autowired
    @Qualifier("jmsConsumerQueueTemplate")
    private JmsTemplate jmsTemplate;

    public void send(final String consumerMsg, Message producerMessage)
            throws JMSException {
        jmsTemplate.send(producerMessage.getJMSReplyTo(),
                new MessageCreator() {
                    public Message createMessage(Session session)
                            throws JMSException {
                        Message msg
                                = session.createTextMessage("ReplyTo " + consumerMsg);
                        return msg;
                    }
                });
    }
}

消费者topic

@Component
public class TopicReceiver1 implements MessageListener {
    public void onMessage(Message message) {
        try {
            System.out.println(((TextMessage) message).getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(ActiveMQ(三)整合spring)