public class JmsUtils {
public static final String HOST_NAME = "192.168.0.63";
public static final String CHANNELNAME = "chan1";
public static final int CCSID = 1381;
public static final String QMGRNAME = "channel1";
}
import javax.jms.JMSException;
import javax.jms.Topic;
import javax.jms.TopicConnection;
public class PubSubUtils extends JmsUtils {
public TopicConnection getConnection()
throws JMSException {
com.ibm.mq.jms.MQTopicConnectionFactory factory = new
com.ibm.mq.jms.MQTopicConnectionFactory();
factory.setHostName(this.HOST_NAME);
factory.setCCSID(this.CCSID);
factory.setChannel(this.CHANNELNAME);
factory.setQueueManager(this.QMGRNAME);
return factory.createTopicConnection();
}
public Topic getTopic(String name) throws JMSException {
return new com.ibm.mq.jms.MQTopic(name);
}
}
import java.io.IOException;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TopicConnection;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.NamingException;
public class StandalonePublisher {
private PubSubUtils utils;
private TopicConnection connection;
private TopicSession session;
private TopicPublisher publisher;
public static void main(String[] args)
throws NamingException, JMSException, IOException {
StandalonePublisher publisher = new StandalonePublisher();
publisher.connect();
String message = "ignored";
while (message.length() > 0) {
byte[] input = new byte[40];
System.out.print("Enter a message: ");
System.in.read(input);
message = (new String(input, 0, input.length)).trim();
if (message.length() > 0)
publisher.sendMessage(message);
}
publisher.disconnect();
}
private StandalonePublisher() {
utils = new PubSubUtils();
}
private void connect() throws NamingException, JMSException {
connection = utils.getConnection();
session =
connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
publisher = session.createPublisher(utils.getTopic("hello"));
System.out.println("Publisher started.");
}
private void sendMessage(String text) throws JMSException {
Message message = session.createTextMessage(text);
publisher.publish(message);
System.out.println(
"Published message <"
+ text
+ "> with ID <"
+ message.getJMSMessageID()
+ ">");
}
private void disconnect() throws JMSException {
publisher.close();
session.close();
connection.close();
System.out.println("Publisher stopped.");
}
}
import java.io.IOException;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.TopicConnection;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.NamingException;
public class StandaloneSubscriber implements MessageListener {
private PubSubUtils utils;
private TopicConnection connection;
private TopicSession session;
private TopicSubscriber subscriber;
public static void main(String[] args)
throws NamingException, JMSException, IOException {
StandaloneSubscriber subscriber = new StandaloneSubscriber();
subscriber.connect();
System.in.read();
subscriber.disconnect();
}
private StandaloneSubscriber() {
utils = new PubSubUtils();
}
private void connect() throws NamingException, JMSException {
connection = utils.getConnection();
session =
connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
subscriber = session.createSubscriber(utils.getTopic("hello"));
subscriber.setMessageListener(this);
connection.start();
System.out.println("Subscriber started.");
}
public void onMessage(Message message) {
try {
TextMessage tMessage = (TextMessage) message;
String text;
text = tMessage.getText();
System.out.println(
"Received message <"
+ text
+ "> with ID <"
+ message.getJMSMessageID()
+ ">");
} catch (JMSException e) {
e.printStackTrace();
}
}
private void disconnect() throws JMSException {
subscriber.close();
session.close();
connection.stop();
connection.close();
System.out.println("Subscriber stopped.");
}
}