我已經在使用Java的Akka框架中實現了一個應用程序。我有一個主角通過使用'Ask'方法並在60秒後超時來調用子角色,一旦從主角接收消息,工作人員調用另一個Java類方法。Subactor(worker)仍然在Java Akka的主演員超時之後工作
現在的問題是,雖然我的主演員在60秒後超時,但工作人員仍然可以與java類方法進行交談,然後該方法執行的操作不是必需的,因爲主要參與者無法接收由於超時而由子角色返回的響應。
是否有反正我可以殺死工人或阻止它進一步處理,如果我的主要演員超時? 我檢查了RecieveTimeOut()
,context.stop()
和poisonpill等方法,但仍然沒有用。
感謝您的支持
下面的代碼
public class MainActor extends UntypedActor {
ActorRef subActorRef;
final Timeout timeout = new Timeout(Duration.create(60, TimeUnit.SECONDS));
@Override
public void preStart() {
subActorRef = getContext().actorOf(
SpringExtProvider.get(actorSystem).props(
"SubActor"), "subActor");
}
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof BusinessRequestVO) {
BusinessRequestVO requestVo = (BusinessRequestVO) message;
ArrayList<Future<Object>> responseFutures = new ArrayList<Future<Object>>();
// This part of code timeout after 60seconds
responseFutures.add(ask(subActorRef,requestVo, timeout));
}
}
}
SubActor類
public class SubActor extends UntypedActor {
@Resource
@Inject
ServiceAdapter serviceAdapter;
@Override
public void onReceive(Object message) throws Exception {
try{
if (message instanceof BusinessRequestVO) {
BusinessRequestVO requestVo = (BusinessRequestVO)message
// There is no time out here so it waits synchronously
// though Main actor timeouts
ServiceResponse response = serviceAdapter.getWorkOrder(requestVo);
getSender().tell(response, ActorRef.noSender());
} catch (Exception e) {
getSender().tell(new akka.actor.Status.Failure(e), getSelf());
throw e;
}
}
}
}
適配器類別
public class ServiceAdapterImpl implements ServiceAdapter{
public ServiceResponse getWorkOrder(BusinessRequestVO request){
// Some code here along with webservice calls
}
}
發佈您的代碼。 – Ryan 2014-12-05 23:35:00
'..從進一步處理..停頓',你的意思是你想中斷'.getWorkOrder()'呼叫在飛行中? – tariksbl 2014-12-07 14:59:01
是的我想打斷.getWorkOrder(),如果家長演員超時 – 2014-12-08 05:51:58