// Path: architecture-core/src/main/java/com/ea/core/kryo/ISerializer.java
// public interface ISerializer extends Serializable{
// 	
// 	/**
// 	 * 序列化成字符串，字符串以ISO-8859-1编码
// 	 * 目前框架里所有序列化的内容都以字符串方式存在
// 	 * 
// 	 * @param obj
// 	 * @return
// 	 * @throws Exception
// 	 */
// 	public String serialize(Object obj) throws Exception;
// 	
// 	/**
// 	 * 序列化成byte数组
// 	 * 
// 	 * @param obj
// 	 * @return
// 	 * @throws Exception
// 	 */
// 	public byte[] Serialize(Object obj) throws Exception;
// 	
// 	/**
// 	 * 根据字符串内容反序列化成对象
// 	 * 
// 	 * @param serialString
// 	 * @return
// 	 * @throws Exception
// 	 */
// 	public Object deserialize(String serialString) throws Exception;
// 	
// 	public Object Deserialize(byte[] aryByte) throws Exception;
// 	
// 	/**
// 	 * 注册Class
// 	 * 
// 	 * @param type
// 	 */
// 	public void register(Class<?> type);
// 	
// 	public void register(Class<?> type, Serializer<?> serializer);
// }
// 
// Path: architecture-core/src/main/java/com/ea/core/mq/consumer/AbstractConsumer.java
// public abstract class AbstractConsumer implements IConsumer {
// 	private Logger logger = LoggerFactory.getLogger(AbstractConsumer.class);
// 	private MQClient client = null;
// 	private String queueName;
// 	
// 	/**
// 	 * 
// 	 */
// 	private static final long serialVersionUID = 1L;
// 
// 	public AbstractConsumer(){
// 		client = new MQClient(MQDefinition.getPropertyValue("mq.server.host"), MQDefinition.getPropertyValue("mq.server.username"), MQDefinition.getPropertyValue("mq.server.password"));
// 	}
// 	@Override
// 	public void setQueueName(String queueName) {
// 		this.queueName = queueName;
// 	}
// 	
// 	@Override
// 	public void run() {
// 		// TODO Auto-generated method stub
// 		try {
// 			execute();
// 		} catch (Exception e) {
// 			//异常处理（策略是记录原始取到的队列信息和异常信息，并写到队列里，队列名为errorQueue）
// 			//
// 		}
// 	}
// 	
// 	protected void execute() throws Exception {
// 		byte[] queueContent = client.receiveMessage(queueName);
// 		if(queueContent == null){
// 			return;
// 		}
// 		ReceiveDTO request = deserialize(queueContent);
// 		logger.info("从队列" + queueName + "收到消息，该消息将交由" + request.getRequestId() + "处理！");
// 		perform(request.getRequestId(), request.getParams());
// 	}
// 	
// 	/**
// 	 * 反序列化，默认反序列化方式在DefaultReceive实现
// 	 * @param aryByte
// 	 * @return
// 	 * @throws Exception
// 	 */
// 	protected abstract ReceiveDTO deserialize(byte[] aryByte) throws Exception;
// 	
// 	protected abstract Object perform(String facadeName, Object... models) throws Exception;
// 	
// }


import com.ea.core.base.context.AppServerBeanFactory;
import com.ea.core.base.request.Request;
import com.ea.core.facade.IFacade;
import com.ea.core.kryo.ISerializer;
import com.ea.core.kryo.impl.DefaultSerializer;
import com.ea.core.kryo.impl.JavaSerializer;
import com.ea.core.kryo.impl.ObjectSerializer;
import com.ea.core.mq.consumer.AbstractConsumer;
import com.ea.core.mq.consumer.ReceiveDTO;
/**
 * Copyright 2014 伊永飞
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ea.core.achieve.mq.consumer;


public class DefaultConsumer extends AbstractConsumer {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2267877435951577268L;
	
	@Override
	protected Object perform(String facadeName, Object... models) throws Exception {
		IFacade facade = (IFacade) AppServerBeanFactory.getBeanFactory().getBean(facadeName);
		return facade.facade(models);
	}

	@Override
	protected ReceiveDTO deserialize(byte[] aryByte) throws Exception {
		// TODO Auto-generated method stub
		ReceiveDTO dto = new ReceiveDTO();
		Request request = deserializeRequest(aryByte);
		dto.setRequestId(request.getRequestId());
		if(request.getContent() != null){
			dto.setParams((Object[])deserializeContent(request));
		}
		return dto;
	}
	
	protected Request deserializeRequest(byte[] aryByte) throws Exception{
		ISerializer serializer = new DefaultSerializer();
		serializer.register(Request.class);
		return (Request)serializer.Deserialize(aryByte);
	}
	
	protected Object deserializeContent(Request request) throws Exception{
// Path: architecture-core/src/main/java/com/ea/core/kryo/ISerializer.java
// public interface ISerializer extends Serializable{
// 	
// 	/**
// 	 * 序列化成字符串，字符串以ISO-8859-1编码
// 	 * 目前框架里所有序列化的内容都以字符串方式存在
// 	 * 
// 	 * @param obj
// 	 * @return
// 	 * @throws Exception
// 	 */
// 	public String serialize(Object obj) throws Exception;
// 	
// 	/**
// 	 * 序列化成byte数组
// 	 * 
// 	 * @param obj
// 	 * @return
// 	 * @throws Exception
// 	 */
// 	public byte[] Serialize(Object obj) throws Exception;
// 	
// 	/**
// 	 * 根据字符串内容反序列化成对象
// 	 * 
// 	 * @param serialString
// 	 * @return
// 	 * @throws Exception
// 	 */
// 	public Object deserialize(String serialString) throws Exception;
// 	
// 	public Object Deserialize(byte[] aryByte) throws Exception;
// 	
// 	/**
// 	 * 注册Class
// 	 * 
// 	 * @param type
// 	 */
// 	public void register(Class<?> type);
// 	
// 	public void register(Class<?> type, Serializer<?> serializer);
// }
// 
// Path: architecture-core/src/main/java/com/ea/core/mq/consumer/AbstractConsumer.java
// public abstract class AbstractConsumer implements IConsumer {
// 	private Logger logger = LoggerFactory.getLogger(AbstractConsumer.class);
// 	private MQClient client = null;
// 	private String queueName;
// 	
// 	/**
// 	 * 
// 	 */
// 	private static final long serialVersionUID = 1L;
// 
// 	public AbstractConsumer(){
// 		client = new MQClient(MQDefinition.getPropertyValue("mq.server.host"), MQDefinition.getPropertyValue("mq.server.username"), MQDefinition.getPropertyValue("mq.server.password"));
// 	}
// 	@Override
// 	public void setQueueName(String queueName) {
// 		this.queueName = queueName;
// 	}
// 	
// 	@Override
// 	public void run() {
// 		// TODO Auto-generated method stub
// 		try {
// 			execute();
// 		} catch (Exception e) {
// 			//异常处理（策略是记录原始取到的队列信息和异常信息，并写到队列里，队列名为errorQueue）
// 			//
// 		}
// 	}
// 	
// 	protected void execute() throws Exception {
// 		byte[] queueContent = client.receiveMessage(queueName);
// 		if(queueContent == null){
// 			return;
// 		}
// 		ReceiveDTO request = deserialize(queueContent);
// 		logger.info("从队列" + queueName + "收到消息，该消息将交由" + request.getRequestId() + "处理！");
// 		perform(request.getRequestId(), request.getParams());
// 	}
// 	
// 	/**
// 	 * 反序列化，默认反序列化方式在DefaultReceive实现
// 	 * @param aryByte
// 	 * @return
// 	 * @throws Exception
// 	 */
// 	protected abstract ReceiveDTO deserialize(byte[] aryByte) throws Exception;
// 	
// 	protected abstract Object perform(String facadeName, Object... models) throws Exception;
// 	
// }


// Path: architecture-achieve/src/main/java/com/ea/core/achieve/mq/consumer/DefaultConsumer.java
import com.ea.core.base.context.AppServerBeanFactory;
import com.ea.core.base.request.Request;
import com.ea.core.facade.IFacade;
import com.ea.core.kryo.ISerializer;
import com.ea.core.kryo.impl.DefaultSerializer;
import com.ea.core.kryo.impl.JavaSerializer;
import com.ea.core.kryo.impl.ObjectSerializer;
import com.ea.core.mq.consumer.AbstractConsumer;
import com.ea.core.mq.consumer.ReceiveDTO;

/**
 * Copyright 2014 伊永飞
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ea.core.achieve.mq.consumer;


public class DefaultConsumer extends AbstractConsumer {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2267877435951577268L;
	
	@Override
	protected Object perform(String facadeName, Object... models) throws Exception {
		IFacade facade = (IFacade) AppServerBeanFactory.getBeanFactory().getBean(facadeName);
		return facade.facade(models);
	}

	@Override
	protected ReceiveDTO deserialize(byte[] aryByte) throws Exception {
		// TODO Auto-generated method stub
		ReceiveDTO dto = new ReceiveDTO();
		Request request = deserializeRequest(aryByte);
		dto.setRequestId(request.getRequestId());
		if(request.getContent() != null){
			dto.setParams((Object[])deserializeContent(request));
		}
		return dto;
	}
	
	protected Request deserializeRequest(byte[] aryByte) throws Exception{
		ISerializer serializer = new DefaultSerializer();
		serializer.register(Request.class);
		return (Request)serializer.Deserialize(aryByte);
	}
	
	protected Object deserializeContent(Request request) throws Exception{
		ISerializer serializer = (ISerializer) new JavaSerializer(ObjectSerializer.class).deserialize(request.getSerializer());