@Override
 public void run() {
   running = true;
   finished = false;
   NioSocketConnector connector = setupConnector(clientHandler);
   IoSession session = createSession(connector, socketAddress);
   while (running) {
     String processedObservation = null;
     try {
       processedObservation = queue.poll(500, TimeUnit.MILLISECONDS);
     } catch (InterruptedException e) {
       logger.error("Interrupted while polling the processed observation queue");
     }
     if (processedObservation != null) session.write(processedObservation);
   }
   session.getCloseFuture().awaitUninterruptibly(); // closed when idle in handler
   connector.dispose();
   finished = true;
 }
示例#2
0
  public static void main(String[] args) {
    // 创建一个非阻塞的客户端程序
    IoConnector connector = new NioSocketConnector();
    // 设置链接超时时间
    connector.setConnectTimeout(30000);
    // 设置过滤器
    connector
        .getFilterChain()
        .addLast(
            "codec",
            new ProtocolCodecFilter(
                new TextLineCodecFactory(
                    Charset.forName("UTF-8"),
                    LineDelimiter.WINDOWS.getValue(),
                    LineDelimiter.WINDOWS.getValue())));

    // 添加业务逻辑处理器类
    connector.setHandler(new Demo1ClientHandler());
    IoSession session = null;
    try {
      ConnectFuture future = connector.connect(new InetSocketAddress(HOST, PORT)); // 创建连接
      future.awaitUninterruptibly(); // 等待连接创建完成
      session = future.getSession(); // 获得session

      String sendPhone = "13681803609"; // 当前发送人的手机号码
      String receivePhone = "13721427169"; // 接收人手机号码
      String message = "测试发送短信,这个是短信信息哦,当然长度是有限制的哦....";
      String msg = sendPhone + ";" + receivePhone + ";" + message;

      session.write(msg); // 发送给移动服务端
    } catch (Exception e) {
      logger.error("客户端链接异常...", e);
    }

    session.getCloseFuture().awaitUninterruptibly(); // 等待连接断开
    connector.dispose();
  }
示例#3
0
 public boolean close() {
   CloseFuture future = session.getCloseFuture();
   future.awaitUninterruptibly(1000);
   connector.dispose();
   return true;
 }
示例#4
0
 @Override
 public Object invoke(Invocation invocation) throws Throwable {
   session.getCloseFuture().setClosed();
   return null;
 }