首页 > 其他分享 >guava servicemanager 集成guice 使用

guava servicemanager 集成guice 使用

时间:2022-10-18 19:36:20浏览次数:75  
标签:guice void class Override protected servicemanager new guava public

实例代码

  • servicemanager
public class MyServiceManagerProvider implements Provider<ServiceManager> {
   // 此处初始化一个AbstractService 实际上是多余的, ServiceManager 自带一个NoOpService
    @Inject
    Set<Service> services = Sets.<Service>newHashSet(new AbstractService() {
        @Override
        protected void doStart() {
        }
 
        @Override
        protected void doStop() {
 
        }
    });
 
    @Override
    public ServiceManager get() {
        return new ServiceManager(services);
    }
}
  • 注册
public class BootstrapModule extends AbstractModule {
 
    @Override
    protected void configure() {
        Multibinder<Service> serviceMultibinderBinder = Multibinder.newSetBinder(binder(), Service.class);
        serviceMultibinderBinder.addBinding().to(ServiceA.class);
        serviceMultibinderBinder.addBinding().to(ServiceB.class);
        bind(ServiceManager.class).toProvider(MyServiceManagerProvider.class).asEagerSingleton();;
    }
}
  • 使用
@Singleton
public class Init {
    @Inject
    private  ServiceManager serviceManager;
    public ServiceManager start(){
        return serviceManager.startAsync();
    }
}
  • 启动
 public static void main(String[] args) throws InterruptedException {
        Injector injector = Guice.createInjector(new BootstrapModule());
        Init init=  injector.getInstance(Init.class);
        ServiceManager serviceManager = init.start();
        serviceManager.servicesByState().get(Service.State.RUNNING).stream().forEach(service -> System.out.println(service.isRunning()));
        Thread.sleep(10000);
        serviceManager.stopAsync();
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            try {
                serviceManager.awaitStopped(10, TimeUnit.SECONDS);
            } catch (TimeoutException e) {
                throw new RuntimeException(e);
            }
        }));
    }
   

service 类型

 

 

使用中的一些问题

一般我们实现AbstractIdleService,对于定时任务实现AbstractScheduledService以及AbstractExecutionThreadService(单线程模式)
对于直接实现AbstractService的服务,需要自定编写线程管理服务,否则运行效果会不符合实际预期
参考实现

 
public class ServiceC extends AbstractService {
    @Override
    protected void doStart() {
        MoreExecutors.directExecutor().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("servicec do starting");
                    notifyStarted();
                } catch (Throwable t) {
                    notifyFailed(t);
                }
            }
        });
    }
 
    @Override
    protected void doStop() {
        MoreExecutors.directExecutor().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("serviceb do stoping");
                    notifyStopped();
                } catch (Throwable t) {
                    notifyFailed(t);
                }
            }
        });
    }

AbstractExecutionThreadService实际上的效果是执行之后如果状态是runing 的可以自己处理,比如循环数据处理,如果running 部分很快结束了,然后执行也就执行了

private final Service delegate =
      new AbstractService() {
        @Override
        protected final void doStart() {
          Executor executor =
              MoreExecutors.renamingDecorator(
                  executor(),
                  new Supplier<String>() {
                    @Override
                    public String get() {
                      return serviceName();
                    }
                  });
          executor.execute(
              new Runnable() {
                @Override
                public void run() {
                  try {
                    startUp();
                    notifyStarted();
                    // If stopAsync() is called while starting we may be in the STOPPING state in
                    // which case we should skip right down to shutdown.
                    if (isRunning()) {
                      // 一般我们都是在isRunning 中进行业务处理
                      try {
                        AbstractExecutionThreadService.this.run();
                      } catch (Throwable t) {
                        try {
                          shutDown();
                        } catch (Exception ignored) {
                          // TODO(lukes): if guava ever moves to java7, this would be a good
                          // candidate for a suppressed exception, or maybe we could generalize
                          // Closer.Suppressor
                          logger.log(
                              Level.WARNING,
                              "Error while attempting to shut down the service after failure.",
                              ignored);
                        }
                        notifyFailed(t);
                        return;
                      }
                    }
 
                    shutDown();
                    notifyStopped();
                  } catch (Throwable t) {
                    notifyFailed(t);
                  }
                }
              });
        }

参考使用

public class ServiceD extends AbstractExecutionThreadService {
    @Override
    protected void run() throws Exception {
        System.out.println("ServiceD waitting to running");
       // 此处也可能是一个循环,或者基于信号量
        if(isRunning()){
            System.out.println("ServiceD running");
        }
    }
 
    @Override
    protected void shutDown() throws Exception {
        super.shutDown();
        System.out.println("ServiceD shutdown");
    }
 
    @Override
    protected void startUp() throws Exception {
        super.startUp();
        System.out.println("ServiceD startUp");
    }
 
    @Override
    protected void triggerShutdown() {
        super.triggerShutdown();
        System.out.println("ServiceD triggerShutdown");
    }
}

graylog 的实现

 

 

说明

对于复杂服务状态管理我们基于guava servicemanager 是一个不错的选择,比如graylog 的service 就比较依赖servicemanager,对于我们进行服务管理是一个很不错的选择,值得使用

参考资料

https://github.com/google/guava/wiki/ServiceExplained

标签:guice,void,class,Override,protected,servicemanager,new,guava,public
From: https://www.cnblogs.com/rongfengliang/p/16803762.html

相关文章

  • guava使用
    引入依赖<dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>29.0-jre</version></dependency>代码案例importc......
  • guice AssistedInject 简单说明
    guiceAssistedInject主要的场景是解决一个对象的创建同时依赖一个实例访问以及ioc容器,通用的解决方法是通过工厂方法,因为比较常见,所以guice提供了一个扩展,可以解决类......
  • GuavaCache中LoadingCache的使用
    背景LoadingCache是GuavaCache构建缓存实体的方法,是一个支持多线程并发读写、高性能、通用的in-heap(堆)本地缓存。支持key不存在时按照给定的CacheLoader的loader方法......
  • Guava Retry重试机制
    1、添加pom依赖<dependency><groupId>com.github.rholder</groupId><artifactId>guava-retrying</artifactId><version>2.0.0</version></dependency>2、Gua......
  • Guava中这些Map的骚操作,让我的代码量减少了50%
    Guava是google公司开发的一款Java类库扩展工具包,内含了丰富的API,涵盖了集合、缓存、并发、I/O等多个方面。使用这些API一方面可以简化我们代码,使代码更为优雅,另一方面它补......
  • 除了Guava,Java开发者还值得了解的5个谷歌类库
    Guava是谷歌基于Java1.6的一个类库集合的扩展项目,包含了许多谷歌核心的Java常用库。除开Guava,还有一些谷歌的常用Java类库也值得我们关注,本文列出了其中的五个类库......
  • Guava常用工具类总结
    Guava常用工具类总结-"我想写得更优雅,可是没人告诉我怎么写得更优雅"-"Null的含糊语义让人很不舒服。Null很少可以明确地表示某种语义,例如,Map.get(key)返回Null时,可能表......