ActiveMQ五种消息的发送/接收

 1.生产者

//连接工厂
ConnectionFactory connFactory = new ActiveMQConnectionFactory(
		ActiveMQConnection.DEFAULT_USER,
		ActiveMQConnection.DEFAULT_PASSWORD,
		"tcp://localhost:61616");
 
//连接到JMS提供者
Connection conn = connFactory.createConnection();
conn.start();
 
//事务性会话,自动确认消息
Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
 
//消息的目的地
Destination destination = session.createQueue("queue.hello");
 
//消息生产者
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); //不持久化
 
 
//文本消息
TextMessage textMessage = session.createTextMessage("文本消息");
producer.send(textMessage);
 
//键值对消息
MapMessage mapMessage = session.createMapMessage();
mapMessage.setLong("age", new Long(32));
mapMessage.setDouble("sarray", new Double(5867.15));
mapMessage.setString("username", "键值对消息");
producer.send(mapMessage);
 
//流消息
StreamMessage streamMessage = session.createStreamMessage();
streamMessage.writeString("streamMessage流消息");
streamMessage.writeLong(55);
producer.send(streamMessage);
 
//字节消息
String s = "BytesMessage字节消息";
BytesMessage bytesMessage = session.createBytesMessage();
bytesMessage.writeBytes(s.getBytes());
producer.send(bytesMessage);
 
//对象消息
User user = new User("cjm", "对象消息"); //User对象必须实现Serializable接口
ObjectMessage objectMessage = session.createObjectMessage();
objectMessage.setObject(user);
producer.send(objectMessage);
 
 
session.commit(); //在事务性会话中,只有commit之后,消息才会真正到达目的地
producer.close();
session.close();
conn.close();

2.消费者

public class Receiver implements MessageListener{
	private boolean stop = false;
	
	public void execute() throws Exception {
		//连接工厂
		ConnectionFactory connFactory = new ActiveMQConnectionFactory(
				ActiveMQConnection.DEFAULT_USER,
				ActiveMQConnection.DEFAULT_PASSWORD,
				"tcp://localhost:61616");
		
		//连接到JMS提供者
		Connection conn = connFactory.createConnection();
		conn.start();
		
		//事务性会话,自动确认消息
		Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
		
		//消息的来源地
		Destination destination = session.createQueue("queue.hello");
		
		//消息消费者
		MessageConsumer consumer = session.createConsumer(destination);
		consumer.setMessageListener(this);
		
		//等待接收消息
		while(!stop){
			Thread.sleep(5000);
		}
		
		session.commit();
		
		consumer.close();
		session.close();
		conn.close();
	}
 
	public void onMessage(Message m) {
		try{
			if(m instanceof TextMessage){ //接收文本消息
				TextMessage message = (TextMessage)m;
				System.out.println(message.getText());
			}else if(m instanceof MapMessage){ //接收键值对消息
				MapMessage message = (MapMessage)m;
				System.out.println(message.getLong("age"));
				System.out.println(message.getDouble("sarray"));
				System.out.println(message.getString("username"));
			}else if(m instanceof StreamMessage){ //接收流消息
				StreamMessage message = (StreamMessage)m;
				System.out.println(message.readString());
				System.out.println(message.readLong());
			}else if(m instanceof BytesMessage){ //接收字节消息
				byte[] b = new byte[1024];
				int len = -1;
				BytesMessage message = (BytesMessage)m;
				while((len=message.readBytes(b))!=-1){
					System.out.println(new String(b, 0, len));
				}
			}else if(m instanceof ObjectMessage){ //接收对象消息
				ObjectMessage message = (ObjectMessage)m;
				User user = (User)message.getObject();
				System.out.println(user.getUsername() + " _ " + user.getPassword());
			}else{
				System.out.println(m);
			}
			
			stop = true;
		}catch(JMSException e){
			stop = true;
			e.printStackTrace();
		}
	}
}

 

你可能感兴趣的:(ActivityMQ,消息队列,java)