Remote Procedure Calls

Shalika Prasad
5 min readJul 17, 2020

We know how to call local methods using any language. that is very simple to invoke that method to your working class. but, if we want to call another method run in another environment, then how do we call to that program. we want a protocol to communicate, which means we want a standard way to use all of the users. So, we can introduce remote procedure calls as a protocol.

Our purpose is to just call the remote methods and get responses quickly. We don’t want to understand network details like where to run. So, the RPC protocol is good for that. It uses a message-based communication schema as a separate environment. As well as, in here, It use well-structured packet data.

This is the sample structure of the client-server procedure. The RPC system always hides the details of the system. I hope to explain more details about this structure using practical code practice. Firstly we see how to work on the server-side.

  1. I hope to publish two endpoints on the server-side. The first endpoint creates just passing name parameters and print hello string. the second endpoint creates a call for Dao object (we can call database), get data, and pass data as an array string to the client.
http://localhost:7780/hello
http://localhost:7780/product

2. This is the main method of server, here publish the endpoints. I used different ports. (7780 for hello interface, 7781 for the product interface). After executing this method, the server always listening to these ports whether the client request to coming or not. After coming client message, identify which method is want to execute and pass parameters to the corresponding interface.

3. This is the hello service interface.

Use @WebService for expressing this interface of 7780 port, and use @SOAPBinding to explain that use RPC style. Use @WebMethod to express this method is called via the interface directly.

4. This is the implementation of the Hello service interface.

After coming request to interface redirect that class to execute method to get results. Use @WebService(endpointInterface = “com.prasad.HelloWorld”) to express which is the corresponding interface of this class. Use @Override to express this is the implementation method of the corresponding interface web method.

5. Now, we see the product server. This is the product interface like hello service.

This is the corresponding implementation class

I used ProductDao class to find products, but I didn’t connect database now, you can connect if you want. I simply generate data and pass results.

I have to use the Product.java entity class for this.

6. Now we see, what is client-side, how to call the server-side.

This is main class of the client-side. After running the server-side, you can call via browser using http://localhost:7780/hello?wsdl this URL to see details of available in server-side like which methods that you can run.

Specially, when we create interface of client side, we can get details of server side like “namespaceURL”, “localPart”, and which methods can access public . You want to create same package name in client side using namespaceURL. (in this example : com.prasad)

We get URL and using URL interface. QName return methods of RPC. Using QName and URL we can create Service to call Hello Server and Prodct Server.

After creating Service, we can call to get the name of this Service, getPort. Using getPort that we can get the instance of a generated stub implementation class or a dynamic proxy.

We pass ore HelloWorld.class to generate HelloWorld stub implementation class. (HelloWorld hello = serviceHello.getPort(HelloWorld.class);)

After generating this, you can call methods of public available from the server.

hello.getHi("prasad");
hello.getHelloWorldAsString()

7. This is the HelloWorld interface on the client-side. Then we can create interface to execute server site methods.

If you remember, you can see above HelloWorldImpl class has a method “getPrivatePassword”, If you see in XML, you cannot see any details about this method. The client cannot see anything inside the implementation method. If you to execute this method from client-side, you can get like these,

Exception in thread “main” javax.xml.ws.WebServiceException: Method getPrivatePassword is exposed as WebMethod, but there is no corresponding wsdl operation with name {http://prasad.com/}getPrivatePassword in the wsdl:portType{http://prasad.com/}HelloWorld

So, you can see, you cannot call this method via remotely.

Conclusion

I think that you learn more about RPC (Remote Procedure Call). You can get code via GitHub using this link.

https://github.com/shalikaprasad/java-rpc.git

Thank you…

--

--