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");
}
}