Commit 68e0f79f authored by 耿迪迪's avatar 耿迪迪

原型模式修改udp服务

parent 01d8b83c
package com.zehong.gassdevicereport.netty;
import com.zehong.gassdevicereport.netty.handler.FactoryDetectorUdpServerHandler;
import com.zehong.gassdevicereport.netty.handler.UDPServerHandler;
import com.zehong.gassdevicereport.netty.property.UdpStartProperty;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
/**
* @author geng
* upd服务启动类
*/
@Component
public class UdpServerStart implements ApplicationContextAware {
private Logger logger = LoggerFactory.getLogger(UdpServerStart.class);
private ApplicationContext applicationContext;
private Map<Integer,Class> handlerMap = new HashMap<Integer,Class>(){{
put(65011,UDPServerHandler.class);
//工业报警器
put(65012,FactoryDetectorUdpServerHandler.class);
}};
@Value("${UDPNetty.ports}")
private int[] ports;
@PostConstruct
public void udpStart(){
UdpStartProperty property = new UdpStartProperty();
for(int port : ports){
try {
property.setPort(port);
SimpleChannelInboundHandler handler = (SimpleChannelInboundHandler)applicationContext.getBean(handlerMap.get(port));
property.setHandler(handler);
UdpStartProperty udpServer = (UdpStartProperty) property.clone();
udpServer.start();
} catch (CloneNotSupportedException e) {
logger.error("udp 原型创建失败!");
}
}
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
package com.zehong.gassdevicereport.netty.property;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author geng
* upd服务原型类
*/
public class UdpStartProperty implements Cloneable{
private Logger logger = LoggerFactory.getLogger(UdpStartProperty.class);
private SimpleChannelInboundHandler handler;
private int port;
/**
* 启动udp服务
*/
public void start(){
NioEventLoopGroup group = new NioEventLoopGroup();
//2.启动器
Bootstrap bootstrap = new Bootstrap();
//3.配置启动器
//3.1指定group
bootstrap.group(group)
//3.2指定channel
.channel(NioDatagramChannel.class)
//3.3指定为广播模式
.option(ChannelOption.SO_BACKLOG,128)
.handler(handler);
try {
//4.bind到指定端口,并返回一个channel,该端口就是监听UDP报文的端口
ChannelFuture channel = bootstrap.bind(this.port).sync();
if(channel.isSuccess()){
logger.info("upd服务启动" + this.getPort() +"成功");
}
} catch (InterruptedException e) {
logger.error("upd 服务端口启动失败:" + e);
}
}
public SimpleChannelInboundHandler getHandler() {
return handler;
}
public void setHandler(SimpleChannelInboundHandler handler) {
this.handler = handler;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
@Override
public Object clone() throws CloneNotSupportedException {
Object obj = super.clone();
return obj;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment