Version 3

    What's the JBoss Remoting 3 HelloWorld Sample

     

    JBoss Remoting is wide used in JBoss Application Server, Remoting 3 is the next generation of JBoss Remoting, this article will provide a quick start sample for using JBoss Remoting 3.

     

    More about JBoss Remoting 3 please refer to https://www.jboss.org/jbossremoting/remoting-3

     

    The HelloWorld Sample's architecture like below:

    remoting3_sample.png

    • CharSequenceReceiver play a role as Server, it use JBoss Remoting 3, listen on localhost/30123, it will output every received char sequence.
    • CharSequenceSender play a role as Client, it connect to Server, create a Remoting 3 Connection, use this connection it can send char sequence repeated

     

    Source Code

     

    package org.jboss.remoting3.sample;
    
    import java.io.IOException;
    import java.net.InetSocketAddress;
    
    import org.jboss.remoting3.Channel;
    import org.jboss.remoting3.Endpoint;
    import org.jboss.remoting3.MessageInputStream;
    import org.jboss.remoting3.OpenListener;
    import org.jboss.remoting3.Remoting;
    import org.jboss.remoting3.remote.RemoteConnectionProviderFactory;
    import org.jboss.remoting3.security.SimpleServerAuthenticationProvider;
    import org.jboss.remoting3.spi.NetworkServerProvider;
    import org.xnio.IoUtils;
    import org.xnio.OptionMap;
    import org.xnio.Options;
    import org.xnio.Sequence;
    import org.xnio.channels.AcceptingChannel;
    import org.xnio.channels.ConnectedStreamChannel;
    
    public class CharSequenceReceiver {
    
        private static final int THREAD_POOL_SIZE = 100;
        private static final int BUFFER_SIZE = 8192;
        private static byte[] buffer;
    
        protected final Endpoint serverEndpoint;
    
        private AcceptingChannel<? extends ConnectedStreamChannel> server;
    
        public CharSequenceReceiver() throws IOException {
            serverEndpoint = Remoting.createEndpoint("connection-test-server", OptionMap.create(Options.WORKER_TASK_CORE_THREADS, THREAD_POOL_SIZE, Options.WORKER_TASK_MAX_THREADS, THREAD_POOL_SIZE));
            serverEndpoint.addConnectionProvider("remote", new RemoteConnectionProviderFactory(), OptionMap.create(Options.SSL_ENABLED, Boolean.FALSE));
            final NetworkServerProvider networkServerProvider = serverEndpoint.getConnectionProviderInterface("remote", NetworkServerProvider.class);
            SimpleServerAuthenticationProvider provider = new SimpleServerAuthenticationProvider();
            provider.addUser("bob", "test", "pass".toCharArray());
            server = networkServerProvider.createServer(new InetSocketAddress("localhost", 30123), OptionMap.create(Options.SASL_MECHANISMS, Sequence.of("CRAM-MD5")), provider, null);
            System.out.println("Server Created, " + server.getLocalAddress());
    
            serverEndpoint.registerService("test", new OpenListener(){
    
                public void channelOpened(Channel channel) {
                    channel.receiveMessage(new Channel.Receiver(){
    
                        public void handleError(Channel channel, IOException error) {
    
                        }
    
                        public void handleEnd(Channel channel) {
    //                        System.out.println(channel.getConnection().getRemoteEndpointName() + " ended");
                        }
    
                        public void handleMessage(Channel channel, MessageInputStream message) {
                            try {
                                channel.receiveMessage(this);
                                buffer = new byte[BUFFER_SIZE];
                                while (message.read(buffer) > -1);
                                System.out.println("    Receive: " + new String(buffer));
                            } catch (Exception e) {
                                e.printStackTrace();
                            } finally {
                                IoUtils.safeClose(message);
                            }
                        }
                    });
                }
    
                public void registrationTerminated() {
    
                }}, OptionMap.EMPTY);
        }
    
    
        public static void main(String[] args) throws IOException {
            new CharSequenceReceiver();
        }
    
    }
    

     

     

    package org.jboss.remoting3.sample;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.InetSocketAddress;
    
    import org.jboss.remoting3.Channel;
    import org.jboss.remoting3.Connection;
    import org.jboss.remoting3.Endpoint;
    import org.jboss.remoting3.MessageOutputStream;
    import org.jboss.remoting3.Registration;
    import org.jboss.remoting3.Remoting;
    import org.jboss.remoting3.remote.RemoteConnectionProviderFactory;
    import org.xnio.IoFuture;
    import org.xnio.IoUtils;
    import org.xnio.OptionMap;
    import org.xnio.Options;
    
    public class CharSequenceSender {
    
        private static final Integer THREAD_POOL_SIZE = 100;
    
        protected final Endpoint clientEndpoint;
    
        protected final Registration clientReg;
    
        protected final Connection conn;
    
        public CharSequenceSender() throws IOException {
            clientEndpoint = Remoting.createEndpoint("connection-test-client", OptionMap.create(Options.WORKER_TASK_CORE_THREADS, THREAD_POOL_SIZE, Options.WORKER_TASK_MAX_THREADS, THREAD_POOL_SIZE));
            clientReg = clientEndpoint.addConnectionProvider("remote", new RemoteConnectionProviderFactory(), OptionMap.create(Options.SSL_ENABLED, Boolean.FALSE));
            conn = clientEndpoint.connect("remote", new InetSocketAddress("localhost", 0), new InetSocketAddress("localhost", 30123), OptionMap.EMPTY, "bob", "test", "pass".toCharArray()).get();
            System.out.println("Connection created, " + conn.getEndpoint().getName());
            send();
        }
    
    
        private void send() {
            System.out.println("enter 'quit' or 'exit' exit the ChatDemo");
    
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    
            while (true) {
                try {
                    String line = in.readLine().toLowerCase();
                    if (line.startsWith("quit") || line.startsWith("exit")) {
                        break;
                    }
    
                    MessageOutputStream stream = null;
                    Channel channel = null;
    
                    try {
                        final IoFuture<Channel> future = conn.openChannel("test", OptionMap.EMPTY);
                        channel = future.get();
                        stream = channel.writeMessage();
                        stream.write(new String(line.getBytes(), "UTF-8").getBytes());
                    } catch (Exception e) {
                        throw new RuntimeException("send char sequence error", e);
                    } finally {
                        stream.close();
                        IoUtils.safeClose(channel);
                    }
    
                } catch (Exception e) {
                    throw new RuntimeException("send char sequence error", e);
                }
            }
    
            IoUtils.safeClose(clientReg);
            IoUtils.safeClose(clientEndpoint);
        }
    
    
        public static void main(String[] args) throws IOException {
            new CharSequenceSender();
        }
    
    }
    

     

    Source code in github https://github.com/kylinsoong

     

    Run and Test

     

    • Clone Source code from github, or download Source code from attachment, use maven build:

     

    mvn clean install dependency:copy-dependencies
    

     

    • Start CharSequenceReceiver

     

    java -cp target/dependency/*:target/jboss-remoting-test.jar org.jboss.remoting3.sample.CharSequenceReceiver
    

     

    • Start CharSequenceSender

     

    java -cp target/dependency/*:target/jboss-remoting-test.jar org.jboss.remoting3.sample.CharSequenceSender
    

     

    • Type char sequence in client side as below:

    VM-remoting-test-client.png

    • Check From Server side 3 char sequence be received by server

    VM-remoting-test-server.png