1.核心工厂配置类

@Component
public class HandlerContext {

private Map<String, AbstractHandler> handlerMap;

private HandlerContext() {
}

private void init(Map<String, AbstractHandler> handlerMap) {
this.handlerMap = handlerMap;
}

public AbstractHandler getInstance(String type) {
AbstractHandler handler = handlerMap.get(type);
if (handler == null) {
System.out.println("没有type为‘" + type + "’的处理器");
throw new IllegalArgumentException("not found handler for type: " + type);
}
return handler;
}

//初始化处理器
@Bean
private Map<String, AbstractHandler> initHandlerContext(Map<String, AbstractHandler> handlerMap){
this.init(handlerMap);
return this.handlerMap;
}

}

2.抽象处理父类
public abstract class AbstractHandler {

protected static final Logger log = LoggerFactory.getLogger(AbstractHandler.class);

@Autowired
protected HandlerThreadPool handlerThreadPool;
@Autowired
protected RedisTemplate redisTemplate;

public abstract String handler(ChannelHandlerContext ctx, NettyMessage message);

}

3.子类写法 需使用spring的service注解
@Service("base")
public class BaseHandler extends AbstractHandler

@Service("mls")
public class GameMLSHandler extends AbstractHandler

等等等·····

4. 使用
AbstractHandler handler = handlerContext.getInstance(message.getType());
handler.handler(ctx, message);

5.线程池数组
可根据角标指定哪一个线程去处理,可解决部分多次发送等多线程问题。
@Component
public class HandlerThreadPool {

//线程池数组
private static ExecutorService[] handlerThreadPool = new ExecutorService[5];

public HandlerThreadPool() {
for (int i = 0; i < handlerThreadPool.length; i++) {
handlerThreadPool[i] = Executors.newFixedThreadPool(1);
}
}

public ExecutorService getHandlerThread(int index){
return index > handlerThreadPool.length ? null : handlerThreadPool[index];
}

public static int getThreadSize(){
return handlerThreadPool.length;
}
}
以上的NettyMessage为自定义消息类,需根据具体业务自行编写


记一次自定义管理工厂使用spring自动装载bean的更多相关文章

  1. Spring自动装配bean

    Spring推荐面向接口编程,这样可以很好的解耦具体的实现类. CompactDisc.class 文件: public interface CompactDisc { void play(); } ...

  2. Spring 自动装配 Bean

    Spring3系列8- Spring 自动装配 Bean 1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiri ...

  3. Spring自动装配Bean详解

    1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiring ‘byType 4.      Auto-Wirin ...

  4. Spring深入浅出(二)IOC的单例 ,继承,依赖,JDBC,工厂模式以及自动装载

    IOC的单例模式--Bean Spring中的bean是根据scope来决定的. scope有4种类型: 1.singleton:单例模型,表示通过Spring容器获取的该对象是唯一的.常用并且默认. ...

  5. Spring核心技术(八)——Spring自动装载的注解

    本文针对自动装载的一些注解进行描述. 基于注解的容器配置 @Required注解 @Required注解需要应用到Bean的属性的setter方法上面,如下面的例子: public class Sim ...

  6. Spring自动装配Bean的五种方式

    在Spring中,支持 5 自动装配模式. no – 缺省情况下,自动配置是通过“ref”属性手动设定,在项目中最常用byName – 根据属性名称自动装配.如果一个bean的名称和其他bean属性的 ...

  7. Spring自动注入Bean

    通过@Autowired或@Resource来实现在Bean中自动注入的功能,但还要在配置文件中写Bean定义,下面我们将介绍如何注解Bean,从而从XML配置文件 中完全移除Bean定义的配置. 1 ...

  8. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring自动装配Bean

    除了使用 XML 和 Annotation 的方式装配 Bean 以外,还有一种常用的装配方式——自动装配.自动装配就是指 Spring 容器可以自动装配(autowire)相互协作的 Bean 之间 ...

  9. spring 自动装配 bean 有哪些方式?

    Spring容器负责创建应用程序中的bean同时通过ID来协调这些对象之间的关系.作为开发人员,我们需要告诉Spring要创建哪些bean并且如何将其装配到一起. spring中bean装配有两种方式 ...

  10. SPRING IN ACTION 第4版笔记-第二章-001-用@Autowired\@ComponentScan、@Configuration、@Component实现自动装载bean

    1. package soundsystem; import org.springframework.context.annotation.ComponentScan; import org.spri ...

随机推荐

  1. npm i不成功devDependencies解决方法

    npm config ls -l 查看npm配置发现production为true,所以i不成功 npm config set production false 将production设置为false ...

  2. 摹客演示Axure原型,适配更丰富机型

    Hi!各位小伙伴!又到了摹客的新功能播报时间.本次更新,对Axure原型的演示进行了优化,支持预览不同分辨率的布局:在设计规范方面,非编辑者的界面标识也更加清晰:另外还有一些细节体验的优化.下面就一起 ...

  3. python中如何获取主机的ip和主机名

    使用python中的socket库,可以轻松获取主机ip和主机名. 一.获取主机名 import socket hostname = socket.gethostname() print(hostna ...

  4. python 迁移虚拟环境

    1.在源环境中获取包列表(新建文件夹whls) #cd 虚拟环境目录下的\scripts,cmd acitivate # 下载清单到requirements.txt,切换到whls目录 pip fre ...

  5. R8051_simulation

    1 下载 git clone https://github.com/risclite/R8051.git 2 编辑文件 mkdir work && mv sim tb flist .. ...

  6. DDD(二)聚合、聚合根、领域服务、应用服务、仓储”和“工作单元”、领域事件、集成事件

    DDD(二)聚合.聚合根.领域服务.应用服务.仓储"和"工作单元".领域事件.集成事件 如果觉得样式不好:跳转即可 http://www.lifengying.site/ ...

  7. css - object-fit ie兼容

    css - object-fit ie兼容 参考资料 github 解决object-fit兼容IE浏览器实现图片自适应 demo <!-- * @createDate: 2022-08-30 ...

  8. TCP三次握手和四次挥手的原因所在

    报文从运用层传送到运输层,运输层通过TCP三次握手和服务器建立连接,四次挥手释放连接. 为什么需要三次握手呢?为了防止已失效的连接请求报文段突然又传送到了服务端,因而产生错误. 比如:client发出 ...

  9. CSS3-transform位移实现双开门效果

    transform可以用于实现位移,旋转,缩放等效果. 位移:transform: translate(水平距离,垂直距离); 这里先借助其位移属性实现双开门的效果,鼠标hover的时候最上面的图片向 ...

  10. 替代学习物联网-云服务-03腾讯云MQTT

    1.登录(利用微信) https://console.cloud.tencent.com/iothub 2.新建产品 3.添加设备 4.设备详细参数 域名IP固定: iotcloud-mqtt.gz. ...