Search in sources :

Example 1 with ChannelHandlerAdapter

use of com.alibaba.dubbo.remoting.transport.ChannelHandlerAdapter in project dubbo by alibaba.

the class PeerMain method main.

public static void main(String[] args) throws Throwable {
    // Group address, supporting two groups of multicast and file, extensible
    String groupURL = "multicast://224.5.6.7:9911";
    // The native server address for cross networking
    final String peerURL = "dubbo://0.0.0.0:" + (((int) (Math.random() * 10000)) + 20000);
    // Join the group and get the peer reference
    Peer peer = Networkers.join(groupURL, peerURL, new ChannelHandlerAdapter() {

        @Override
        public void received(Channel channel, Object message) throws RemotingException {
            System.out.println("Received: " + message + " in " + peerURL);
        }
    });
    // Sending messages to other peers in the network
    for (int i = 0; i < Integer.MAX_VALUE; i++) {
        // Access to channels with all other peers, this list changes dynamically
        Collection<Channel> channels = peer.getChannels();
        if (channels != null && channels.size() > 0) {
            for (Channel channel : channels) {
                // Sending messages to a specified peer
                channel.send("(" + i + ") " + peerURL);
            }
        }
        Thread.sleep(1000);
    }
    // leave the network
    peer.leave();
}
Also used : ChannelHandlerAdapter(com.alibaba.dubbo.remoting.transport.ChannelHandlerAdapter) Channel(com.alibaba.dubbo.remoting.Channel) RemotingException(com.alibaba.dubbo.remoting.RemotingException)

Aggregations

Channel (com.alibaba.dubbo.remoting.Channel)1 RemotingException (com.alibaba.dubbo.remoting.RemotingException)1 ChannelHandlerAdapter (com.alibaba.dubbo.remoting.transport.ChannelHandlerAdapter)1