Pages

Friday, 30 January 2015

Creating web socket service in JDeveloper

Usecase:

This blog post talks about creating web socket based services in JDeveloper. At the end of this post, you will be familiar with creating a basic web socket based service that takes in a path parameter and prints out a message for you.

Pre-requisites:

JDeveloper 12.1.3.0.0

Steps:
  • Create a new custom application 'WebSocketSample' in JDeveloper. In Step 2 of the wizard, select the project feature as 'Web Socket' and toggle it to the right. Once you do this, you will see Web Socket as well as java listed out in the RHS. Finish the wizard.


  • Next, create a new java class. Call it 'WebSocketWithParam'. Call the package 'websocket'. In this java class, we will write the code for the socket service. We will have three methods with 'name' as parameters. These parameters are configured as PathParams as shown below. We will add the import for them in a while.
  1. package websocket;
    
    public class WebSocketWithParam {
        public String helloThere(@PathParam("yourname") String name){
        return "Hello There "+name;
        }
    
        public String yourmessage(@PathParam("yourname") String name, String msg){
        return "From "+name+" Message: "+msg;
        }
    
        public String bye(@PathParam("yourname") String name){
        return "Bye There "+name;
        }
    }
  • Focus on the class name and using PI, select the check box Web Socket Service. This will generate the annotation @ServerEndpoint("service"). Remember to append a '/' before the name of the ServerEndPoint. ie., make it @ServerEndpoint("/service"). Focus on helloThere method and using PI, check the checkbox for OnOpen Method.
    Focus on yourmessage method and using PI, check the checkbox for OnMessage Method. Now focus on the parameter 'name' present on this method and in PI, add 'yourname' as Web Socket Client. Also, modify @ServerEndpoint("/service") to @ServerEndpoint("/service/{yourname}")

    Focus on bye method and using PI, check the checkbox for OnClose Method. Also fix the import for PathParam by adding the import javax.websocket.server.PathParam.

             Final code:
  • package websocket;
    
    import javax.websocket.OnClose;
    import javax.websocket.OnMessage;
    import javax.websocket.OnOpen;
    import javax.websocket.server.PathParam;
    import javax.websocket.server.ServerEndpoint;
    
    @ServerEndpoint("/service/{yourname}")
    public class WebSocketWithParam {
        @OnOpen
        public String helloThere(@PathParam("yourname") String name){
        return "Hello There "+name;
        }
    
        @OnMessage
        public String yourmessage(@PathParam("yourname") String name, String msg){
        return "From "+name+" Message: "+msg;
        }
    
        @OnClose
        public String bye(@PathParam("yourname") String name){
        return "Bye There "+name;
        }
    }
  • The websocket is now ready. Right click on the class 'WebSocketWithParam' and select Run. A target URL will be generated for you in the weblogic server window, as shown below:
  • Clicking on this target URL should result in a dialog 'Connected Successfully'.
  • To test if the service is behaving as expected, I recommend that you use 'Dark WebSocket Terminal' available for free on the chrome store. Feed in the generated target URL in the terminal and enter a message. On sending the request, you should be able to see the response received from the web socket.


No comments:

Post a Comment