All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.dubboclub.akka.remoting.actor.ServiceProvider Maven / Gradle / Ivy

There is a newer version: 0.0.4
Show newest version
package net.dubboclub.akka.remoting.actor;

import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.japi.Creator;
import akka.routing.ActorRefRoutee;
import com.alibaba.dubbo.rpc.*;
import net.dubboclub.akka.remoting.codec.AkkaCodec;
import net.dubboclub.akka.remoting.message.Request;
import net.dubboclub.akka.remoting.message.RequestPackage;
import net.dubboclub.akka.remoting.message.Response;
import net.dubboclub.akka.remoting.message.ResponsePackage;

/**
 * Created by bieber on 2015/7/8.
 */
public class ServiceProvider extends RouterActor {

    private Invoker invoker;

    private volatile int currentWorkerSize = 0;

    public ServiceProvider(Invoker invoker){
        super(invoker.getUrl());
        this.invoker = invoker;
    }

    @Override
    public void preStart() throws Exception {
        super.preStart();
        logging.info("start service {}", getSelf().path());
        for(int i=0;i{

        private Invoker invoker;

        public WorkerCreator(Invoker invoker) {
            this.invoker = invoker;
        }

        @Override
        public Worker create() throws Exception {
            return new Worker(invoker);
        }
    }

    static class Worker extends UntypedActor{
        private Invoker invoker;

        protected Worker(Invoker invoker){
            this.invoker = invoker;
        }
        @Override
        public void onReceive(Object o) throws Exception {
            RequestPackage requestPackage = (RequestPackage) o;
            Response response = new Response(requestPackage.getmId());
            Request request = null;
            try{
                request = (Request) AkkaCodec.decode(invoker.getUrl(),requestPackage.getRequestMessage());
            }catch (Exception e){
                wrapErrorResponse(Response.SERVER_ERROR,e,response);
            }
            Object data = request.getmData();
            if(data instanceof Invocation){
                Invocation invocation = (Invocation) request.getmData();
                try{
                    Result result = invoker.invoke(invocation);
                    response.setResult(result);
                    response.setStatus(Response.OK);
                }catch (Throwable e){
                    wrapErrorResponse(Response.SERVICE_ERROR,e,response);
                }
            }else{
                RpcException e = new RpcException("unsupported request "+data);
                wrapErrorResponse(Response.SERVER_ERROR,e,response);
            }
            ResponsePackage responsePackage = new ResponsePackage(response.getId(),AkkaCodec.encode(invoker.getUrl(),response).array());
            getSender().tell(responsePackage,getSelf());
        }

    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy