您的位置:首页 > 其它

Akka入门

2016-07-19 09:08 162 查看
最近看到公司的项目用到了akka,不知道是个什么东西,百度之,发现是个有关并发的东西。
首先看代码。
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;

import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.*;

@Log4j
@Service
public class AsyncService {
ActorSystem system = ActorSystem.create("actorSystem");
private ActorRef actor = system.actorOf(Props.create(GeneralActor.class), "generalActor");

@Autowired
private GlobalSetting globalSetting;
@Autowired
private UserService userService;
@Autowired
private ActivityService activityService;
@Autowired
private LiveInfoService liveInfoService;

@Autowired
private JavaMailSender javaMailSender;
@Autowired
private MailService mailSendExecutor;
public void asyncHandle(Map<String, Object> data, int messageType) {
AsyncMessage message = new AsyncMessage();
message.setType(messageType);
message.setData(data);
actor.tell(message, null);
}

public void handle(Object message) {
AsyncMessage asyncMessage = (AsyncMessage) message;
Map<String, Object> data = asyncMessage.getData();
switch (asyncMessage.getType()) {
case AsyncMessage.TYPE_EMAIL:
mailSendExecutor.sendEmail(data);
break;
default:
}
}

}


import akka.actor.UntypedActor;

public class GeneralActor extends UntypedActor {

private AsyncService asyncService;

public void onReceive(Object message) throws Exception
{
try
{
getService().handle(message);
}
catch (Exception ex)
{}
}

private AsyncService getService()
{
if(null ==asyncService)
{
asyncService = SpringContextHelper.getBean(AsyncService.class);
}
return asyncService;
}

}


看完才发现,在某个方法里面调用了AsyncService的asyncHandle方法,然后这个方法用到了akka,使用一个actor通知到了GeneralActor,这个Actor又调用AsyncService的handle方法,这个方法真正发送了一个邮件,就是拿来发个邮件啊,大材小用了啊,不知道谁写的啊。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: