java后台异步处理

按我做的项目,简单记录,公司内部封装,其他地方不适用:

①在需要异步调用时,写好调用方法,并传异步处理需要的参数:

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<EventRedPackages> {
	
	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<String,Object> data1 = new HashMap<String,Object>();
			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;
    }
}





  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Happy王子乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值