JMS监听MQ实例

jsm_applicationContext.xml配置
<!-- 配置jms参数 -->
<bean id="QmembfeQmConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
  <property name="queueManager">
    <value>QMEMBFE</value>
  </property>
  <property name="hostName">
    <value>182.119.170.83</value>
  </property>
  <property name="transportType">
    <value>1</value>
  </property>
  <property name="port">
    <value>1417</value>
  </property>
  <property name="CCSID">
    <value>819</value>
  </property>
  <property name="channel">
    <value>SYSTEM.DEF.SVRCONN</value>
  </property>
</bean>
<!-- 配置jms的发送管理队列 -->
<bean id="sendQueue" class="com.ibm.mq.jms.MQQueue">
  <property name="baseQueueName">
    <value>3012900001_5</value>
  </property>
  <property name="targetClient">
    <value type="int">1</value>
  </property>
</bean>
<!-- 配置jms的接收管理队列 -->
<bean id="acceptQueue" class="com.ibm.mq.jms.MQQueue">
  <property name="baseQueueName">
    <value>3012900001_4</value>
  </property>
  <property name="targetClient">
    <value type="int">1</value>
  </property>
</bean>
<!-- 固定配置开始 -->
<bean id="jmsTransactionManager" class="org.springframework.jms.connection.JmsTransactionManager">
  <property name="connectionFactory">
    <ref bean="QmembfejmsConnectionFactory" />
  </property>
</bean>
<bean id="QmembfejmsConnectionFactory" class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
  <property name="targetConnectionFactory" ref="QmembfeQmConnectionFactory" />
  <property name="username" value="" />
  <property name="password" value=" " />
</bean>
<bean id="txProxyTemplateJms" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
  <property name="transactionManager" ref="jmsTransactionManager" />
  <property name="transactionAttributes">
    <props>
      <prop key="send*">PROPAGATION_REQUIRED</prop>
      <prop key="get*">PROPAGATION_REQUIRED</prop>
      <prop key="process*">PROPAGATION_REQUIRED</prop>
    </props>
  </property>
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate" >
  <property name="connectionFactory">
    <ref bean="QmembfejmsConnectionFactory"/>
  </property>
</bean>
<!-- 固定配置结束 -->
<bean id="jmsManager" class="com.jms.manager.JmsManager">
  <property name="jmsTemplate">
    <ref bean="jmsTemplate"/>
  </property>
</bean>


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)