import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.SerializationException;
import org.apache.commons.lang.SerializationUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import cn.cbsw.entity.QyBumen;
import cn.cbsw.rabbitmq.RbChannel;
import cn.cbsw.rabbitmq.RbConnection;
import cn.cbsw.redis.token.TokenModel;
import cn.cbsw.tool.StringUtils;
import cn.cbsw.tran.daoimp.DanweiDao;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
/**
* 定义发送数据切面
* @author Administrator
*
*/
@Aspect // for aop
@Component // for auto scan
public class DataSend {
private static String host = "127.0.0.1"; //haproxy在71的8100端口
@Autowired
DanweiDao danweiDao;
/**
* 返回通知:目标方法正常执行完毕时执行以下代码
* @param jp
* @param result
*/
@AfterReturning(value="execution(* cn.cbsw.tran.serviceimp.*.insert*(..))"
+ " or execution(* cn.cbsw.tran.serviceimp.*.update*(..))"
+ " or execution(* cn.cbsw.tran.serviceimp.*.delete*(..))"
+ " or execution(* cn.cbsw.tran.serviceimp.*.execute*(..))",returning="result")
public void afterReturningMethod(JoinPoint jp, Object result){
Signature signature=jp.getSignature();
String methodName = signature.getName();
sendData(jp.getArgs(),methodName,"");//发送数据
//System.out.println("getDeclaringType:"+signature.getDeclaringType().getSimpleName());
System.out.println("【返回通知】the method 【" + methodName + "】 ends with 【" + result + "】");
}
/**
* 发送数据至rabbiteMQ
* @param o
* @param type
* @param dataType
* @param areaCode
*/
private void sendData(Object[] args,String type,String dataType){
System.out.println(getAreaCodeAndData(args)[1]);
String[] areaCodeAndData=getAreaCodeAndData(args);
String areaCode=areaCodeAndData[0];
if(StringUtils.isBlank(areaCode))
return;
Map<String, Object> map=new HashMap<>();
map.put("type", type);
map.put("data", areaCodeAndData[1]);
map.put("dataType", areaCodeAndData[2]);
//发送数据
directSend("yzb.4100", "queue", new Gson().toJson(map));
}
/**
* 发送数据
*/
private void directSend(String exchangerName,String queue,String data){
try {
//链接
RbConnection connection = new RbConnection(host, 5672, "guest", "guest", "");
//通道
RbChannel rbChannel = connection.getDirectChannel(exchangerName, queue, null);
rbChannel.send(data, (Map<String, Object>)null);
System.out.println("发送成功");
rbChannel.close(true);//关闭连接,后台进程结束
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取单位所在辖区和需要发送的数据
* @param args
* @return
*/
private String[] getAreaCodeAndData(Object[] args){
String compId="",compCode="",data="",dataTypa="";
List<Object> paras=new ArrayList<>(Arrays.asList(args));
Iterator<Object> iterator=paras.iterator();
while(iterator.hasNext()){
Object o=iterator.next();
//根据tokenModel确认当前数据属于哪个单位
if(o instanceof TokenModel){
TokenModel tokenModel=(TokenModel) o;
compId=tokenModel.getCompId();
compCode=danweiDao.getDanweiAreaCode(compId);
iterator.remove();
}
/**
* 判断数据属于哪个分类,然后发送该数据
* 使用枚举数据,或者系统参数设置
*/
if(o instanceof QyBumen){
data=new Gson().toJson(o);
dataTypa="qyBumen";
}
//如果都已经有数据了,则跳出
if(StringUtils.isNotBlank(compCode)&&StringUtils.isNotBlank(data))
break;
}
String[] areaCodeAndData={compCode,data,dataTypa};
return areaCodeAndData;
}
public static void main(String[] args) {
try {
// 链接
RbConnection connection = new RbConnection(host, 5672, "guest", "guest", "");
// 通道
RbChannel rbChannel = connection.getFanoutChannel("yzb.4100", "queue", 1);
final boolean autoAck = rbChannel.getAutoAck();
// 接收响应,如果需要用到channel,使用rbChannel.getChannel()取代null
DefaultConsumer consumer = new DefaultConsumer(rbChannel.getChannel()) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
System.out.println("consumerTag: " + consumerTag);
String s = null;
try {
s = (String) SerializationUtils.deserialize(body);
} catch (SerializationException e) {
// e.printStackTrace();
s = new String(body, "utf-8");
}
System.out.println("收到内容:" + s);
// Gson gson=new Gson();
// Map<String, Object> map=gson.fromJson(s,new TypeToken<Map<String, Object>>(){}.getType());
//QyBumen bumen=gson.fromJson(StringUtils.objectToString(map.get("data")), QyBumen.class);
//System.out.println(bumen.getBmName());
if (!autoAck) {
this.getChannel().basicAck(envelope.getDeliveryTag(), true);
}
}
};
rbChannel.push(consumer);
System.out.println("over");
} catch (Exception e) {
e.printStackTrace();
}
}
}