websphere mq jms调用通信demo

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class JMSMessageHander implements MessageListener{

	@Override
	public void onMessage(Message message) {
		
		try {
			TextMessage msg = (TextMessage)message;
			System.out.println(msg.getText());
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	
}


import java.io.IOException;
import java.util.Hashtable;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.ibm.mq.MQException;

public class MQ_JmsUtil {
	
	
	public static  Context jndiContext;   
	
	public static void putMessageToQueue( String queueConnectionFactoryName, String queueName ) 
			throws MQException, IOException, JMSException, NamingException{
		if( jndiContext == null ){
			Hashtable<String, String> env = new Hashtable<String, String>();   
	        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");   
	        env.put(Context.PROVIDER_URL, "file:/C:/Users/issuser/jndi");  
			jndiContext = new InitialContext(env);
		}
		ConnectionFactory queueConnectionFactory = (ConnectionFactory)jndiContext.lookup(queueConnectionFactoryName);
		Connection queueConnection = queueConnectionFactory.createConnection();
		Session session = queueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		MessageProducer producer = session.createProducer((Queue)jndiContext.lookup(queueName));
		TextMessage textMsg = session.createTextMessage(" hello mqjms "); 
		
		producer.send(textMsg);
		
		session.close();
		queueConnection.close();
	}
	
	public static void getMessageFromQueue( String queueConnectionFactoryName, String queueName) 
			throws MQException, IOException, JMSException, NamingException{
		if( jndiContext == null ){
			Hashtable<String, String> env = new Hashtable<String, String>();   
	        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");   
	        env.put(Context.PROVIDER_URL, "file:/C:/Users/issuser/jndi");  
			jndiContext = new InitialContext(env);
		}
		ConnectionFactory queueConnectionFactory = (ConnectionFactory)jndiContext.lookup(queueConnectionFactoryName);
		Connection queueConnection = queueConnectionFactory.createConnection();
		Session session = queueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		
		MessageConsumer consumer = session.createConsumer((Queue)jndiContext.lookup(queueName));
		
		consumer.setMessageListener(new JMSMessageHander());
		queueConnection.start();
	}
	
	
	public static void putMessageToTopic( String topicConnectionFactoryName, String topicName )
			throws MQException, IOException, JMSException, NamingException{
		if( jndiContext == null ){
			Hashtable<String, String> env = new Hashtable<String, String>();   
	        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");   
	        env.put(Context.PROVIDER_URL, "file:/C:/Users/issuser/jndi");  
			jndiContext = new InitialContext(env);
		}
		ConnectionFactory topicConnectionFactory = (ConnectionFactory)jndiContext.lookup(topicConnectionFactoryName);
		Connection topicConnection = topicConnectionFactory.createConnection();
		Session session = topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		
		MessageProducer producer = session.createProducer((Topic)jndiContext.lookup(topicName));
		TextMessage textMsg = session.createTextMessage(" hello mqjms "); 
		
		producer.send(textMsg);
		
		session.close();
		topicConnection.close();
		
	}
	
	
	
	public static void getMessageFromTopic(String topicConnectionFactoryName, String topicName ) 
			throws MQException, IOException, JMSException, NamingException{
		if( jndiContext == null ){
			Hashtable<String, String> env = new Hashtable<String, String>();   
	        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");   
	        env.put(Context.PROVIDER_URL, "file:/C:/Users/issuser/jndi");  
			jndiContext = new InitialContext(env);
		}
		ConnectionFactory topicConnectionFactory = (ConnectionFactory)jndiContext.lookup(topicConnectionFactoryName);
		Connection topicConnection = topicConnectionFactory.createConnection();
		topicConnection.setClientID("ypqiao");
		Session session = topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		
		
		TopicSubscriber subscriber = session.createDurableSubscriber((Topic)jndiContext.lookup(topicName), "asjdlfjsaf");

		subscriber.setMessageListener(new JMSMessageHander());
		
		topicConnection.start();
		
	}
	
	public static void main(String[] args) throws MQException, IOException, JMSException, NamingException{
		//putMessageToQueue("queueConnectionFactory","queue");
		//getMessageFromQueue("queueConnectionFactory","queue");
		
		//putMessageToTopic("topicConnectionFactory","topic");
		//getMessageFromTopic("topicConnectionFactory","topic");
		
	}
	
	

}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值