netty线程[nioEventLoopGroup-3-2](已暂停)

时间:2019-05-23 14:09:25

标签: netty

我使用的是netty 4,我有一个AuthenticateHandler,它扩展了ChannelInboundHandlerAdapter并提供了updateConnection服务。

问题是当ChannelInboundHandlerAdapter使用该服务时,什么也没发生。

public class AuthenticateHandler extends ChannelInboundHandlerAdapter {
    ServiceDeviceServer service;

    public AuthenticateHandler(ServiceDeviceServer serviceCore) {
        service = serviceCore;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("serveur netty Classe AuthenticateHandler et methode  channelRead");

        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();

        DeviceServerContext context = gson.fromJson((String) msg, DeviceServerContext.class);
        //  System.out.println(context);
        Device device = service.findByMei(Long.valueOf(context.getNmea().getMei()));
        context.setDevice(device);

        if (null == device) {
            context.setMessageError(Consts.NOTEXIST);
            ChannelFuture future = ctx.writeAndFlush(gson.toJson(context));
        } else {
            String message = validateDevice(device);
            if (!message.isEmpty()) {
                context.setMessageError(message);
                ChannelFuture future = ctx.writeAndFlush(gson.toJson(context));
            }
            else {
                service.updateConnection(device.getId(),"sds");
            context.setGpsConnection(service.updateConnection(device.getId(),
                    ((InetSocketAddress) ctx.channel().localAddress()).getHostName()));
            ctx.fireChannelRead(gson.toJson(context));
            }
        }
    }

    private String validateDevice(Device device) {
        String msg = "";
        if (!device.isActive()) {
            return msg = "Gps client is inactive. Received data will be pending for unitId '" + device.getUnitId()
                    + "'";
        }

        if (!device.getModel().equals(Consts.SUPPORTEDMODEL)) {
            msg = "Device client GPS model " + device.getModel() + " unitId  " + device.getUnitId()
                    + " is not supported for server";
        }
        return msg;
    }
}


@Component("serviceDeviceServer") 
public class ServiceDeviceServer { 
    @Autowired
    private GpsConnectionDao gpsConnectionDao;

    @Autowired
    private DeviceInfoDAO deviceInfoDAO;

    @Autowired
    private DeviceDAO deviceDao;

    public GpsConnection updateConnection(Long id, String address) {
        GpsConnection gpsConnection = null;
        try {
            if (gpsConnectionDao.existsById(id)) {
                Optional<GpsConnection> gpsConnectionFind = gpsConnectionDao.findById(id);
                gpsConnection = gpsConnectionFind.get();    
            }   
            else {
                gpsConnection = new GpsConnection();
                gpsConnection.setIdGps(id);
            }
            gpsConnection.setLastAuthentication(new Date());
            gpsConnection.setIpAddress(address);
            gpsConnectionDao.save(gpsConnection);
        } catch (Exception e) {
            e.printStackTrace();
        } 
        finally {
            System.out.println("dfgdgd");   
        }
        return gpsConnection;
    }
}

0 个答案:

没有答案