Pages

Tuesday, 3 February 2015

Consuming a web socket service through java proxy

Usecase:

This is in continuation to my previous blogpost which talked about creating a web socket service. Here, I will show you how the web socket service can be consumed through a java client.

Pre-requisites:

JDeveloper 12.1.3.0.0

Steps:
  • Create a new custom application. In Step 2 of the wizard, select the project feature as 'Web Socket' and toggle it to the right. You will see Web Socket as well as java listed out in the RHS. Finish the wizard.
  • Create two java classes ClientClass.java and MainClass.java. Keep them in package 'websocketclient'. Write the following code in MainClass.java
  1. package websocketclient;
    
    import java.io.IOException;
    import java.net.URI;
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.TimeUnit;
    import javax.websocket.ContainerProvider;
    import javax.websocket.DeploymentException;
    import javax.websocket.Session;
    import javax.websocket.WebSocketContainer;
    
    public class MainClass {
        final static CountDownLatch messageLatch = new CountDownLatch(1);
    
        public static void main(String[] args) throws DeploymentException, IOException {
            WebSocketContainer container = ContainerProvider.getWebSocketContainer();
            String uri = "ws://127.0.0.1:7101/WebSocketSample-Project1-context-root/service/Andy"; //replace this with the web socket url generated for you
            System.out.println("Connecting to " + uri);
            container.connectToServer(ClientClass.class, URI.create(uri));
            try {
                System.out.println("Waiting");
                messageLatch.await(100, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                System.out.println(e);
            }
        }
    }
  • In ClientClass.java, write the below code:
  • package websocketclient;
    
    import java.io.IOException;
    import java.util.List;
    import java.util.Map;
    import javax.websocket.ClientEndpoint;
    import javax.websocket.OnClose;
    import javax.websocket.OnMessage;
    import javax.websocket.OnOpen;
    import javax.websocket.Session;
    
    public class ClientClass {
        public void onOpen(Session session) {
            System.out.println("Connected to endpoint: " + session.getBasicRemote());
            try {
                String msg = "Hello! How are you?";
                System.out.println("Sending message to endpoint: " + msg);
                session.getBasicRemote().sendText(msg);
            } catch (IOException e) {
                System.out.println(e);
            }
        }
    
        public void onMssage(String input, Session session) {
            System.out.println("Received message in client: " + input);
            MainClass.messageLatch.countDown();
            try {
                session.close();
            } catch (IOException e) {
            }
        }
    
        public void onCls(Session session) {
            System.out.println("Closing");
            try {
                session.close();
            } catch (IOException e) {
            }
        }
    }
  • In ClientClass.java, focus on the class name and through PI, select Web Socket Client.

  • Focus on onOpen method and through PI, select onOpen Method. Focus on onMssage method and through PI, select onMessage Method. Focus on onCls method and through PI, select onClose Method.
  • package websocketclient;
    
    import java.io.IOException;
    import java.util.List;
    import java.util.Map;
    import javax.websocket.ClientEndpoint;
    import javax.websocket.OnClose;
    import javax.websocket.OnMessage;
    import javax.websocket.OnOpen;
    import javax.websocket.Session;
    
    @ClientEndpoint
    public class ClientClass {
        @OnOpen
        public void onOpen(Session session) {
            System.out.println("Connected to endpoint: " + session.getBasicRemote());
            try {
                String msg = "Hello! How are you?";
                System.out.println("Sending message to endpoint: " + msg);
                session.getBasicRemote().sendText(msg);
            } catch (IOException e) {
                System.out.println(e);
            }
        }
    
        @OnMessage
        public void onMssage(String input, Session session) {
            System.out.println("Received message in client: " + input);
            MainClass.messageLatch.countDown();
            try {
                session.close();
            } catch (IOException e) {
            }
        }
    
        @OnClose
        public void onCls(Session session) {
            System.out.println("Closing");
            try {
                session.close();
            } catch (IOException e) {
            }
        }
    }
    
  • Run MainClass.java

No comments:

Post a Comment