按我做的项目,简单记录,公司内部封装,其他地方不适用:
①在需要异步调用时,写好调用方法,并传异步处理需要的参数:
EventProcessor.addRedPackagesEvent(id,name, roomId, giftId, giftNum,money);
②在“addRedPackagesEvent”中,写入以下代码,其中包含创建EventRedPackages对象并赋值;调用Handle_RedPackages中的重写方法。
/**
* 生成红包事件
* @param id
* @param roomId
* @param giftId
* @param giftNum
*/
public static void addRedPackagesEvent (String id,String name,String roomId,String giftId,String giftNum,int money) {
try {
disruptorMessageManager.putMessageHandle(
new Handle_RedPackages (
new EventRedPackages()
.setId(id)
.setName(name)
.setRoomId(roomId)
.setGiftId(giftId)
.setGiftNum(giftNum)
.setMoney(money)
));
} catch (Exception e) {
log.error(
"addRedPackagesEvent error. id:" + id + " ,roomId:" + roomId +
" ,giftId:" + giftId + " ,giftNum:" + giftNum + " ,money:" + money, e);
e.fillInStackTrace();
} catch (Throwable e) {
log.error(
"addRedPackagesEvent error. id:" + id + " ,roomId:" + roomId +
" ,giftId:" + giftId + " ,giftNum:" + giftNum + " ,money:" + money, e);
e.fillInStackTrace();
}
}
③创建EventRedPackages对象,其中的set自己手动写返回值,这样赋值时可以连".",代码下部省略
public class EventRedPackages {
private String id;
private String name;
private String roomId;
private String giftId;
private String giftNum;
private int money;
public String getId() {
return id;
}
public EventRedPackages setId(String id) {
this.id = id;
return this;
}...
④写具体走异步的代码,“supper”调用的父类方法,被公司封装了,代码就不贴了。。。
package com.zx.hb.yuebo.event.handle;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zx.hb.async.disruptor.MessageHandle;
import com.zx.hb.yuebo.event.data.EventRedPackages;
import com.zx.hb.yuebo.redis.core.RedisKey;
import com.zx.hb.yuebo.service.ChatroomService;
import com.zx.hb.yuebo.service.LiveRoomCacheService;
import com.zx.hb.yuebo.service.RedPacketsCacheService;
import com.zx.hb.yuebo.utils.SpringUtil;
import com.zx.hb.yuebo.web.Conifg;
/**
* 添加红人审核事件
*
*/
public class Handle_RedPackages extends MessageHandle {
private Logger logger = LoggerFactory.getLogger(this.getClass());
private static final ChatroomService chatroomService = (ChatroomService) SpringUtil.getBean("chatroomService");
private static final LiveRoomCacheService liveRoomCacheService = (LiveRoomCacheService) SpringUtil.getBean("liveRoomCacheService");
private static final RedPacketsCacheService redPacketsCacheService = (RedPacketsCacheService) SpringUtil.getBean("redPacketsCacheService");
public Handle_RedPackages(EventRedPackages data) {
super(data);
}
@Override
public void execute(final EventRedPackages data) {
try {
String listKey = RedisKey.REDS_LIST+data.getGiftId()+data.getId()+System.currentTimeMillis()/1000;
redPacketsCacheService.createRedPackes( data.getId(),data.getName(), data.getRoomId(), data.getGiftId(), listKey, data.getMoney());//生成红包,存redis
Map data1 = new HashMap();
data1.put("listKey",listKey);
data1.put("roomId",data.getRoomId());
chatroomService.sendMsgOneRoom(Long.parseLong(liveRoomCacheService.getChatRoomId(data.getRoomId())),Integer.parseInt(Conifg.redPacketsType()),data1);//生成红包消息
} catch (Exception e) {
logger.error("Handle_RedPackages error"+e);
}
}
}
⑤如果要用代码调用service,还需要用到springUtil,并且在@service后面加上(“×
×
×service”)
package com.zx.hb.yuebo.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
*
*/
public class SpringUtil implements ApplicationContextAware {
/**
*
*/
private static ApplicationContext applicationContext;
/**
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
*
* @param name
* @return
*/
public static Object getBean(String name) {
if (null == applicationContext) {
throw new NullPointerException("applicationContext is null.");
}
return applicationContext.getBean(name);
}
public static Object getBean(@SuppressWarnings("rawtypes") Class className) {
if (null == applicationContext) {
throw new NullPointerException("applicationContext is null.");
}
return applicationContext.getBean("", className);
}
/* (non-Javadoc)
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
*/
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
SpringUtil.applicationContext = applicationContext;
}
}