Zeroc Ice 学习笔记--IceBox

Zeroc Ice 学习笔记--IceBox

1、介绍

   IceBox就好像是一个Tomcat,只需要服务实现Ice.Server接口并能轻松搞定

2、案例(参考Zeroc Ice 权威指南)

    ice文件

Zeroc Ice 学习笔记--IceBox_第1张图片

Zeroc Ice 学习笔记--IceBox_第2张图片

编译Ice文件:

Zeroc Ice 学习笔记--IceBox_第3张图片Zeroc Ice 学习笔记--IceBox_第4张图片Zeroc Ice 学习笔记--IceBox_第5张图片

接口实现:

Zeroc Ice 学习笔记--IceBox_第6张图片

实现方法中连接数据库查询数据或者其他任意操作

服务封装(参考Zeroc Ice权威指南):

public abstract class AbstractIceBoxService implements Service{
    protected Ice.ObjectAdapter objectAdapter;
    protected Identity identity;
    protected static Logger LOG = LoggerFactory.getLogger(AbstractIceBoxService.class);
    protected static Sl4jLogerI icelogger = new Sl4jLogerI("communicator");


    @Override
    public void start(String name, Communicator communicator, String[] args) {
        Ice.Util.setProcessLogger(icelogger);
        objectAdapter = communicator.createObjectAdapter(name);
        Ice.Object object = this.createMyIceServiceObj(args);
        identity = communicator.stringToIdentity(name);
        objectAdapter.add(PerfDispatchInterceptor.addIceObject(identity,object),identity);
        objectAdapter.activate();
        LOG.info(name+" service started ,with param size "+args.length+" detail: "+ Arrays.toString(args));
    }

    @Override
    public void stop() {
       LOG.info(" stopping service "+identity + ".......................");
        objectAdapter.deactivate();
        PerfDispatchInterceptor.removeIceObject(identity);
        LOG.info(" stopped service "+ identity +" stoped");
    }

    //服务配置参数,来自icegrid.xml文件
    public abstract Ice.Object createMyIceServiceObj(String [] args);
}
public class PerfDispatchInterceptor extends DispatchInterceptor{
    private static Logger LOG = LoggerFactory.getLogger(PerfDispatchInterceptor.class);
    private static final Map id2ObjectMAP = new ConcurrentHashMap();
    private static final PerfDispatchInterceptor self = new PerfDispatchInterceptor();

    private static final long serialVersionUID = 1L;

    public static PerfDispatchInterceptor getINSTANCE(){
        return self;
    }

    //添加需要拦截的服务
    public static DispatchInterceptor addIceObject(Identity identity,Object object){
        id2ObjectMAP.put(identity,object);
        return self;
    }

    //此方法可以做任务拦截,雷士AOP
    @Override
    public DispatchStatus dispatch(Request request){
        Identity theId = request.getCurrent().id;
        //request.getCurrent().con 打印出来local address = ip:point服务端信息 换行 remote address = ip:point 客户端信息
        String inf = " dispach req,method: "+request.getCurrent().operation+" service: "+theId.name +" server address: "+request.getCurrent().con;
        LOG.info(inf+" begin");
        try {
            DispatchStatus status = id2ObjectMAP.get(request.getCurrent().id).ice_dispatch(request);
            LOG.info(inf + " success");
            return status;
        }catch (RuntimeException e){
            LOG.error(inf+" error "+e.getMessage());
            throw e;
        }
    }

    public static void removeIceObject(Identity identity){
        LOG.info(" remove ice object "+identity);
        id2ObjectMAP.remove(identity);
    }
}

IceBox服务:

132120_Yiql_2517950.png

3、启动服务

icebox.propertices:修改相对于的IceBox.Service.xxxServices配置

Zeroc Ice 学习笔记--IceBox_第7张图片

Zeroc Ice 学习笔记--IceBox_第8张图片

4、Client调用同上一节

 

转载于:https://my.oschina.net/cxlt216/blog/748498

你可能感兴趣的:(数据库,java)