a simple example rabbitMQ

此rabbitMQ 版本3.2.2:


package com.miracle.queue;

import java.io.IOException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
/**
 * 
 * @author lianan
 * note:init facetory connection
 */
public abstract class EndPoint {
	protected Channel channel;
	protected Connection connection;
	protected String endPointName;
	
	public EndPoint(String endPointName){
		try {
			this.endPointName = endPointName;
			ConnectionFactory factory = new ConnectionFactory();	
			factory.setHost("127.0.0.1");
			factory.setUsername("guest");
	        factory.setPassword("guest");
			factory.setVirtualHost("/");
			connection = factory.newConnection();
			channel = connection.createChannel();
			/**
			 * queue - the name of the queuedurable 
			 * true - if we are declaring a durable queue (the queue will survive a server restart)exclusive 
			 * true - if we are declaring an exclusive queue (restricted to this connection)autoDelete 
			 * true - if we are declaring an autodelete queue (server will delete it when no longer in use)arguments 
			 * other - properties (construction arguments) for the queue
			 */
			channel.queueDeclare(endPointName, true, false, false, null);
		} catch (Exception e) {
			System.out.println("出错了");
			
		}		
		
	}
	
	public void close()throws IOException{
		this.channel.close();
		this.connection.close();
	}
}

package com.miracle.queue;

import java.io.IOException;
import java.io.Serializable;
import org.apache.commons.lang.SerializationUtils;
/**
 * 
 * @author lianan
 * note:生产者,发送消息
 */
public class Producer extends EndPoint{

	public Producer(String endPointName){
		super(endPointName);
		// TODO Auto-generated constructor stub
	}

	public void sendMessage(Serializable serializable)throws IOException{
		channel.basicPublish("", endPointName, null, SerializationUtils.serialize(serializable));
	}
}

package com.miracle.queue;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.SerializationUtils;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.ShutdownSignalException;

/**
 * 
 * @author lianan
 * note:消费者,接收消息
 */
public class QueueConsumer extends EndPoint implements Consumer{
	
	public QueueConsumer(String endPointName)throws IOException{
		super(endPointName);
		
	}

	public void consumer() throws IOException{
		channel.basicConsume(endPointName, true, this);
	}
	

	@Override
	public void handleCancel(String arg0) throws IOException {
		
		
	}

	@Override
	public void handleCancelOk(String arg0) {
		System.out.println("consumer"+arg0+"registered");
		
	}

	@Override
	public void handleConsumeOk(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void handleDelivery(String arg0, Envelope arg1,
			BasicProperties arg2, byte[] arg3) throws IOException {
		Map map=(HashMap)SerializationUtils.deserialize(arg3);
		System.out.println("Message Number "+ map.get("message number") + " received.");
	}

	@Override
	public void handleRecoverOk(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void handleShutdownSignal(String arg0, ShutdownSignalException arg1) {
		// TODO Auto-generated method stub
		
	}
	
	
}

package com.miracle.queue;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;
/**
 * 
 * @author lianan
 * note:测试类
 */
public class Test {
	public static void main(String arg0[]) throws IOException{
		//read queue information,and can thread 
		long t1 = new Date().getTime();
		QueueConsumer consumer = new QueueConsumer("river");
		consumer.consumer();
		
		
		
		//根据queue名称,把info放入队列
		/*Producer producer = new Producer("river"); 
		for (int i = 0; i < 100000; i++) { 
			HashMap message = new HashMap(); 
			message.put("message number", i); 
			producer.sendMessage(message);
			System.out.println("Message Number "+ i +" sent."); 
		} 
		producer.close();*/
		long t2 = new Date().getTime();
		System.out.println(t2-t1);

	}
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值