JMS监听MQ实例

阅读更多
jsm_applicationContext.xml配置


  
    QMEMBFE
  
  
    182.119.170.83
  
  
    1
  
  
    1417
  
  
    819
  
  
    SYSTEM.DEF.SVRCONN
  



  
    3012900001_5
  
  
    1
  



  
    3012900001_4
  
  
    1
  



  
    
  


  
  
  


  
  
    
      PROPAGATION_REQUIRED
      PROPAGATION_REQUIRED
      PROPAGATION_REQUIRED
    
  


  
    
  



  
    
  


JmsManager.java配置
public class JmsManager {
  private JmsTemplate jmsTemplate;
  public void sendMessage(final String msg){
    BeanFactory factory = new ClassPathXmlApplicationContext("jms_applicationContext.xml");
    Destination destination = (Destination)factory.getBean("sendQueue");
    jmsTemplate.send(destination, new MessageCreator(){
      public Message createMessage(Session session) throws JMSException {
        BytesMessage message = session.createBytesMessage();
        try {
          message.writeBytes(msg.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {}
        return message;
      }
    });
  }
  public byte[] getMessage(){
    byte[] msg = (byte[])null;
    BeanFactory factory = new ClassPathXmlApplicationContext("jms_applicationContext.xml");
    Destination destination = (Destination)factory.getBean("acceptQueue");
    Message message = jmsTemplate.receive(destination);
    try{
      if(message instanceof TextMessage){
        msg = ((TextMessage)message).getText().getBytes();
        return msg;
      }
      if(message instanceof BytesMessage){
        BytesMessage bMsg = (BytesMessage)message;
        msg = new byte[(int)bMsg.getBodyLength()];
        bMsg.readBytes(msg);
        return msg;
      }
      if(message instanceof ObjectMessage){
        ObjectMessage oMsg = (ObjectMessage)message;
        msg = (byte[])oMsg.getObject();
        return msg;
      }
    } catch (JMSException e) {
      e.printStackTrace();
    }
    return msg;
  }
}

你可能感兴趣的:(JMS监听MQ实例)