此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);
}
}