一切皆可抽象

大而无形 庖丁解牛 厚积薄发 涤虑玄览
   ::  ::  ::  ::  :: 管理

【原创】mq p-p的java实现

Posted on 2005-09-06 10:27 锋出磨砺 阅读(391) 评论(0)  编辑  收藏 所属分类: 消息中间件

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


只有注册用户登录后才能发表评论。


网站导航: