org.wso2.siddhi.debs2017.transport.utils.TcpNettyClient.java Source code

Java tutorial

Introduction

Here is the source code for org.wso2.siddhi.debs2017.transport.utils.TcpNettyClient.java

Source

package org.wso2.siddhi.debs2017.transport.utils;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.apache.log4j.Logger;
import org.wso2.siddhi.core.event.Event;
import org.wso2.siddhi.tcp.transport.handlers.EventEncoder;
import org.wso2.siddhi.tcp.transport.utils.EventComposite;

import java.util.Arrays;
import java.util.UUID;

/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class TcpNettyClient {

    private EventLoopGroup group;
    private Bootstrap bootstrap;
    private Channel channel;
    private String sessionId;
    private String hostAndPort;

    public TcpNettyClient() {
        group = new NioEventLoopGroup();
        bootstrap = new Bootstrap();
        bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast(new EventEncoder());
            }
        });
    }

    public void connect(String host, int port) {
        // Start the connection attempt.
        try {
            hostAndPort = host + ":" + port;
            channel = bootstrap.connect(host, port).sync().channel();
            sessionId = UUID.randomUUID() + "-" + hostAndPort;
        } catch (InterruptedException e) {
            System.out.println("Error connecting to '" + hostAndPort + "', " + e.getMessage() + "\n" + e);
        }
    }

    public ChannelFuture send(String streamId, Event[] events) {
        EventComposite EventComposite = new EventComposite(sessionId, streamId, events);
        ChannelFuture cf = channel.writeAndFlush(EventComposite);
        cf.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (!future.isSuccess()) {
                    System.out.println("Error sending events to '" + hostAndPort + "' on stream '" + streamId
                            + "', " + future.cause() + ", dropping events " + Arrays.deepToString(events) + "\n"
                            + future.cause());
                }
            }
        });
        return cf;
    }

    public void disconnect() {
        if (channel != null && channel.isOpen()) {
            try {
                channel.close();
                channel.closeFuture().sync();
            } catch (InterruptedException e) {
                System.out.println(
                        "Error closing connection to '" + hostAndPort + "' from client '" + sessionId + "', " + e);
            }
            channel.disconnect();
            System.out.println("Disconnecting client to '" + hostAndPort + "' with sessionId:" + sessionId);
        }
    }

    public void shutdown() {
        disconnect();
        if (group != null) {
            group.shutdownGracefully();
        }
        System.out.println("Stopping client to '" + hostAndPort + "' with sessionId:" + sessionId);
        hostAndPort = null;
        sessionId = null;
    }
}