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 com.ibm.mq.jms.MQQueue;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.mq.*;
public class P2PUtils extends JmsUtils {
public QueueConnection getConnection()
throws JMSException{
com.ibm.mq.jms.MQQueueConnectionFactory factory = new
com.ibm.mq.jms.MQQueueConnectionFactory();
factory.setHostName(this.HOST_NAME);
factory.setCCSID(this.CCSID);
factory.setChannel(this.CHANNELNAME);
factory.setQueueManager(this.QMGRNAME);
return factory.createQueueConnection();
}
public Queue getQueue(String queuename) throws JMSException,MQException {
return new com.ibm.mq.jms.MQQueue(queuename);
}
}
import java.io.IOException;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.QueueConnection;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.NamingException;
import com.ibm.mq.*;
public class StandaloneSender {
private P2PUtils utils;
private QueueConnection connection;
private QueueSession session;
private QueueSender sender;
public static void main(String[] args)
throws NamingException, JMSException, IOException,MQException {
StandaloneSender sender = new StandaloneSender();
sender.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)
sender.sendMessage(message);
}
sender.disconnect();
}
private StandaloneSender() {
utils = new P2PUtils();
}
private void connect() throws JMSException,MQException {
connection = utils.getConnection();
session =
connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
sender = session.createSender(utils.getQueue("t1"));
System.out.println("Sender started.");
}
private void sendMessage(String text) throws JMSException {
Message message = session.createBytesMessage();
sender.send(message);
System.out.println(
"Sent message <"
+ text
+ "> with ID <"
+ message.getJMSMessageID()
+ ">");
}
private void disconnect() throws JMSException {
sender.close();
session.close();
connection.close();
System.out.println("Sender stopped.");
}
}
import java.io.IOException;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.QueueConnection;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.BytesMessage;
import javax.naming.NamingException;
import com.ibm.mq.*;
public class StandaloneReceiver implements MessageListener {
private P2PUtils utils;
private QueueConnection connection;
private QueueSession session;
private QueueReceiver receiver;
public static void main(String[] args)
throws NamingException, JMSException, IOException ,MQException{
StandaloneReceiver receiver = new StandaloneReceiver();
receiver.connect();
System.in.read();
receiver.disconnect();
}
private StandaloneReceiver() {
utils = new P2PUtils();
}
private void connect() throws NamingException, JMSException,MQException {
connection = utils.getConnection();
session =
connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
receiver = session.createReceiver(utils.getQueue("t1"));
receiver.setMessageListener(this);
connection.start();
System.out.println("Receiver started.");
}
public void onMessage(Message message) {
try {
BytesMessage tMessage = (BytesMessage) message;
byte[] bytes = null;
tMessage.readBytes(bytes);
} catch (JMSException e) {
e.printStackTrace();
}
}
private void disconnect() throws JMSException {
receiver.close();
session.close();
connection.stop();
connection.close();
System.out.println("Receiver stopped.");
}
}