rabbitmq(二)

java中调用用rabbitmq实现,首先要两个jar包commons-lang-2.6.jar、rabbitmq-client.jar 生产者和消费者的一些方法先来小试一把,把气氛搞起来让生产者和消费者之间搞起来,更多细节后面慢慢来 什么交换机、什么绑定后面在来一步一步的走

/*
	 * 基本连接及其method介绍
	 * 
	 * 创建连接MQ
	 * ConnectionFactory factor=new ConnectionFactory();
	 * 
	 * 设置MQ所在主机 IP地址
	 * factor.setHost("localhost");
	 * 
	 * 创建一个连接
	 * Connection connection=factor.newConnection();
	 *  
	 * 创建一个频道
	 * Channel channel=connection.createChannel();
	 * 声明队列  (如果在消费者端设置 主要为了防止消息接收者先运行此程序,队列还不存在时创建队列。)
	 * channel.queueDeclare(queue_name,false, false, false, null);
	 * 第一个参数队列名称 
	 * 第二个参数durable(boolean)服务器重启能够存活 
	 * 第三个参数exclusive  是否为当前连接的专用队列,在连接断开后,会自动删除该队列
	 * 第四个参数autodelete:当没有任何消费者使用时,自动删除该队列。、
	 * 第五个参数:其他参数如TTL(队列存活时间)等。
	 * 
	 * 向队里中发送消息
	 * channel.basicPublish("", queue_name, null, message.getBytes());  
	 * 
	 * 创建消费者队列
	 * QueueingConsumer consumer = new QueueingConsumer(channel);
	 * 
	 * 指定消费者队列
	 * channel.basicConsume(queue_name, true,consumer);
	 * 
	 */

1、建立一个连接类(因为消费者和生产连接信息都一样)在让消费者生产者继承它即可;

package com.test.mq;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;


/**
 * 生产者和消费者都用一个 连接
 * @author Administrator
 *
 */
public class RabbitMQUtil {
	
	protected  Channel channel;
	//连接
	protected Connection connection;
	//队里名称
	protected String queue_name;
	
	public RabbitMQUtil(String queue_name) throws Exception{
		
		this.queue_name=queue_name;
		
		//创建连接  和频道  
		ConnectionFactory factor=new ConnectionFactory();
		
		//IP地址
		factor.setHost("localhost");

		//创建连接
		connection=factor.newConnection();
		
		channel=connection.createChannel();
		
		//声明队列
		channel.queueDeclare(queue_name,false, false, false, null);
		
	}
	
	
	/**
	 * 关闭连接
	 * @throws Exception
	 */
	public void close() throws Exception{
		this.channel.close();
		this.connection.close();
	}
	
}

2、建一个生产者


package com.test.mq;

import java.io.IOException;
import java.io.Serializable;

import org.apache.commons.lang.SerializationUtils;

/**
 * 生产者
 * @author Administrator
 */
public class ProducersMQ  extends RabbitMQUtil{
	
	public ProducersMQ(String queue_name) throws Exception {
		super(queue_name);
	}

	/**
	 * 发送数据
	 * @param object
	 * @throws IOException
	 */
	public void sendMessage(Serializable object) throws IOException{
		channel.basicPublish("", queue_name, null, SerializationUtils.serialize(object));
		
	}

}


3、建一个消费者

package com.test.mq;

import java.io.IOException;

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 Administrator
 *
 */
public class QueueConsumer extends ProducersMQ implements Runnable,Consumer {

	public QueueConsumer(String queue_name) throws Exception {
		super(queue_name);
	}

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

	/**
	 * 当消费者注册后自动调用
	 */
	@Override
	public void handleCancelOk(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@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 {
		Message msg = (Message)SerializationUtils.deserialize(arg3);
	    System.out.println("消息是:"+msg.getName()+"------------------"+msg.getContext());
	}

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

	@Override
	public void handleShutdownSignal(String arg0, ShutdownSignalException arg1) {
		
	}

	@Override
	public void run() {
		try {
			channel.basicConsume(queue_name, true,this);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

4、定义一个消息类对象(当然你也可以传字符 其他类型的)我这里传递了个对象

package com.test.mq;

import java.io.Serializable;

/**
 * 消息类  
 * @author Administrator
 *
 */
public class Message implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	private String name;
	
	private String context;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getContext() {
		return context;
	}

	public void setContext(String context) {
		this.context = context;
	}
	
}

5、好了开始测试 生产者 和消费者

package com.test.mq;

/**
 * 生产者发送消息
 * @author Administrator
 *
 */
public class ProducersMain {

	public ProducersMain() throws Exception{
		
		for(int r=0;r<5;r++){
			ProducersMQ producer = new ProducersMQ("queue"+r);
			
			for (int i = 0; i < 5; i++) {
				Message msg=new Message();
				
				msg.setContext("队列【"+r+"】内容"+i);
				msg.setName("名称【"+r+"】:"+i);
				producer.sendMessage(msg);
				//发送消息
				//producer.sendMessage("这是我的第"+i+"消息.");
			}
			producer.close();
		}
		
	}
	
	
	public static void main(String[] args) throws Exception {
		 new ProducersMain();
	}
}

package com.test.mq;

/**
 * 读取队列数据
 * @author Administrator
 *
 */
public class ConsumerMain {

	public ConsumerMain() throws Exception{
		
		for(int r=0;r<5;r++){
			QueueConsumer consumer = new QueueConsumer("queue"+r);
			Thread consumerThread = new Thread(consumer);
			consumerThread.start();	
		}
	}
	
	public static void main(String[] args) throws Exception {
		new ConsumerMain();
	}
}

结果:



好了这样就ok了、先搞出来心情好了自然往下学习的哈哈。。。。。。。。。待续。。。。。。。。。。。。。。。。。。。。。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值