package org.apache.qpid.contrib.json;

import java.io.IOException;

import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.FileBasedConfiguration;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.qpid.contrib.json.processer.EventProcesser;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class ReceiveMessageUtils {

	private Channel channel;

	private Connection connection;
	

	

	/**
	 * 
	 * @param queueName
	 *            队列名称
	 * @param eventProcesser
	 *            处理事件实例
	 * @param clazz
	 *            要接收的消息类型
	 * @throws Exception
	 */
	public void receiveMessage(String queueName, EventProcesser eventProcesser, Class<?> clazz) throws Exception {
		setConfig(null);
		
		

		
		channel = connection.createChannel();
		channel.queueDeclare(queueName, true, false, false, null);
		// System.out.println(" [*] Waiting for messages. To exit press
		// CTRL+C");

		channel.basicQos(1);// 告诉RabbitMQ同一时间给一个消息给消费者
		
		Consumer consumer = new DefaultConsumer(channel) {
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
					byte[] body) throws IOException {
			
			

				String message = new String(body);
				
				if (clazz != null) {
					eventProcesser.process(JSON.parseObject(message, clazz));
				} else {
					eventProcesser.process(JSON.parse(message));
				}

			}
		};

		channel.basicConsume(queueName, true, consumer);

	}

	public ReceiveMessageUtils(Connection connection) {
		super();
		this.connection = connection;
	}

	public void close() throws Exception {

		connection.close();
	}

	
}

@@ -69,417 +69,8 @@
 %0D%0A%0D%0A
-import org.apache.commons.configuration2.Configuration;%0D%0Aimport org.apache.commons.configuration2.FileBasedConfiguration;%0D%0Aimport org.apache.commons.configuration2.PropertiesConfiguration;%0D%0Aimport org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;%0D%0Aimport org.apache.commons.configuration2.builder.fluent.Parameters;%0D%0Aimport org.apache.commons.configuration2.ex.ConfigurationException;%0D%0A
 impo
@@ -499,22 +499,16 @@
 ction;%0D%0A
-%09%0D%0A%0D%0A%09
 %0D%0A%0D%0A%09/**
@@ -797,40 +797,8 @@
  %7B%0D%0A
-%09%09setConfig(null);%0D%0A%09%09%0D%0A%09%09%0D%0A%0D%0A%09%09
 %0D%0A%09%09
@@ -1033,18 +1033,16 @@
 %E6%B6%88%E6%81%AF%E7%BB%99%E6%B6%88%E8%B4%B9%E8%80%85%0D%0A
-%09%09
 %0D%0A%09%09Cons
@@ -1249,18 +1249,8 @@
 n %7B%0D
-%0A%09%09%09%0D%0A%09%09%09%0D
 %0A%0D%0A%09
@@ -1288,20 +1288,16 @@
 body);%0D%0A
-%09%09%09%09
 %0D%0A%09%09%09%09if
@@ -1709,10 +1709,7 @@
 %0D%0A%0D%0A
-%09%0D%0A
 %7D%0D%0A

Update ReceiveMessageUtils.java