少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

package org.abin.lee.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class MapSender {

 private static final int SEND_NUMBER = 5;

 public static void main(String[] args) {
  ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
    ActiveMQConnection.DEFAULT_USER,
    ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
  Connection connection = null;
  Session session;
  Destination destination = null;
  MessageProducer messageProducer;
  try {
   connection=connectionFactory.createConnection();
   connection.start();
   session=connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
   destination=session.createQueue("FirstQueue");
   messageProducer=session.createProducer(destination);
   messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
   
   MapMessage map=session.createMapMessage();
   map.setBoolean("flag", true);
   map.setDouble("dou", 1.01);
   map.setInt("zx", 88);
   map.setString("zifu", "zzzzzz");
   messageProducer.send(map);
   session.commit();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }


}






package org.abin.lee.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MapMessage;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class MapReceiver {public static void main(String[] args) {
 ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
   ActiveMQConnection.DEFAULT_USER,
   ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
 Connection connection = null;
 Session session;
 Destination destination;
 MessageConsumer consumer;
 try {
  connection = connectionFactory.createConnection();
  connection.start();
  session = connection.createSession(Boolean.FALSE,
    Session.AUTO_ACKNOWLEDGE);
  destination = session.createQueue("FirstQueue");
  consumer = session.createConsumer(destination);
  while(true){
   MapMessage map=(MapMessage)consumer.receive(500000);
   if (null != map) {
    boolean flag=map.getBoolean("falg");
    System.out.println("AcitveMQ 接收到的消息  flag="+flag);
    double dou=map.getDouble("dou");
    System.out.println("AcitveMQ 接收到的消息  dou="+dou);
    int zx=map.getInt("zx");
    System.out.println("AcitveMQ 接收到的消息  zx="+zx);
    String zifu=map.getString("zifu");
    System.out.println("AcitveMQ 接收到的消息  zifu="+zifu);
   }else
    break;

  }
  
   

 } catch (Exception e) {
  e.printStackTrace();
 }

}
}

posted on 2012-08-02 16:35 abin 阅读(866) 评论(0)  编辑  收藏 所属分类: ActiveMQ

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


网站导航: