Search in sources :

Example 1 with ExitUnlimitCirclePolicy

use of com.zx.sms.connect.manager.ExitUnlimitCirclePolicy in project SMSGate by Lihuanghe.

the class TestCirculeFutureTask method test.

@Test
public void test() {
    final Thread th = Thread.currentThread();
    EventLoopGroupFactory.INS.submitUnlimitCircleTask(new Callable<Integer>() {

        private long lastime = 0;

        @Override
        public Integer call() throws Exception {
            cnt++;
            long now = System.nanoTime();
            System.out.println(now - lastime);
            lastime = now;
            return cnt;
        }
    }, new ExitUnlimitCirclePolicy() {

        @Override
        public boolean notOver(Future future) {
            try {
                boolean ret = (Integer) future.get() < 100;
                if (!ret) {
                    LockSupport.unpark(th);
                }
                return ret;
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            LockSupport.unpark(th);
            return false;
        }
    }, 0);
    System.out.println(ManagementFactory.getRuntimeMXBean().getName());
}
Also used : ExitUnlimitCirclePolicy(com.zx.sms.connect.manager.ExitUnlimitCirclePolicy) Future(io.netty.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 2 with ExitUnlimitCirclePolicy

use of com.zx.sms.connect.manager.ExitUnlimitCirclePolicy in project SMSGate by Lihuanghe.

the class MessageReceiveHandler method userEventTriggered.

public synchronized void userEventTriggered(final ChannelHandlerContext ctx, Object evt) throws Exception {
    synchronized (lock) {
        if (evt == SessionState.Connect && !inited) {
            EventLoopGroupFactory.INS.submitUnlimitCircleTask(new Callable<Boolean>() {

                @Override
                public Boolean call() throws Exception {
                    long nowcnt = cnt.get();
                    EndpointConnector conn = getEndpointEntity().getSingletonConnector();
                    logger.info("channels : {},Totle Receive Msg Num:{},   speed : {}/s", conn == null ? 0 : conn.getConnectionNum(), nowcnt, (nowcnt - lastNum) / rate);
                    lastNum = nowcnt;
                    return true;
                }
            }, new ExitUnlimitCirclePolicy() {

                @Override
                public boolean notOver(Future future) {
                    inited = getEndpointEntity().getSingletonConnector().getConnectionNum() > 0;
                    return inited;
                }
            }, rate * 1000);
            inited = true;
        }
    }
    ctx.fireUserEventTriggered(evt);
}
Also used : EndpointConnector(com.zx.sms.connect.manager.EndpointConnector) ExitUnlimitCirclePolicy(com.zx.sms.connect.manager.ExitUnlimitCirclePolicy) ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future)

Aggregations

ExitUnlimitCirclePolicy (com.zx.sms.connect.manager.ExitUnlimitCirclePolicy)2 Future (io.netty.util.concurrent.Future)2 EndpointConnector (com.zx.sms.connect.manager.EndpointConnector)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 Test (org.junit.Test)1