Call an existing service from custom service

Posted by Admin on 04-Jul-2008 17:30

I am trying to write a custom service which will call another service that I have deployed in the ESB. What is the best way to approach this problem? I know I can use direct JMS connections. But, I want to avoid the overhead of maintaining connections or pools inside a custom service code. Besides, my custom service is already inside a container; all I have to do is to send a message to ABCD. This is something similar to a step in a process which calls another process .

Thanks,

--Subbu

All Replies

Posted by jtownsen on 07-Jul-2008 01:16

Posted by Admin on 07-Jul-2008 15:38

Jamie -

ABCD is a utility service already in place. MyNewCustomService is making decisions and in one of its decision paths it needs to send a request to ABCD, wait for response, go forward with the next step.

I have not yet figured out if this can be done at a process level or is this too complex for a process.

In any case, my question is more generic - how to call an existing service from custom service code? Is there API or examples for this?

--Subbu

Posted by jtownsen on 08-Jul-2008 03:02

I dare say that you could achieve what you're thinking of doing in a number of ways, but there are two big reasons to not do it.

Firstly, a service should implement a single, self-contained step in a process. If you must do more processing on the message in the same service, you should code it in that service itself. This may mean you need to instantiate extra java objects, etc.

Secondly, a service that calls out to another service in a request/reply fashion could lead to duplicated processing of the message. Let me explain...

A service receives messages on its Entry Endpoint, which is a Sonic MQ topic or queue. The message will not be acknowledged on the SonicMQ layer until the service has completed as sent the message to the Exit Endpoint. If during the processing of the message, the server machine crashes, the message will not be lost as it is still in the Entry Endpoint and it will be picked up next time the service starts.

By calling out to another service in you are effectively going to send a request/reply SonicMQ message. The original message is still unacknowledged in the top-level service Entry Endpoint and a message has been sent to the sub-service Entry Endpoint. If the server machine crashes at this point, both messages will be preserved until the services start up again.

If the sub-service is something like an auditing step, things could get worse. If the sub-service completes successfully, but the top-level service then crashes before sending the message out, the auditing would be written to the database/file/etc, but the step would actually have rolled back.

Some of these issues could be avoided by placing the sub-process in the same container as the top-level service and ensuring that Intra-Container Messaging is enabled, but that's a deployment consideration which you can't necessarily guarantee when you're designing and implementing your custom service. As such, you may end up with strange runtime behaviours.

In addition, if you don't use temporary endpoints for the reply message, you will have to write correlation code to make sure that if two instances of the top-level process call out to the sub-process at the same time, that the correct response it sent back to the request.

From what you describe, you need to implement either a CBR/XCBR or a detour pattern (http://www.psdn.com/library/entry!default.jspa?externalID=4319), but the logic for for which branch to take may be extremely complex. In that case, I would suggest coding MyNewCustomService to simply create a new header in the message that can then be used for extremely quick routing at the following CBR/XCBR step.

Posted by Admin on 08-Jul-2008 08:32

Thanks for the excellent explanation Jamie.


The detour pattern has opened up some ideas.

If I may ask - how different is this scenario different from a process with a step calling a sub-process, where the sub-process may or may not be in the same container/server/geographic location ?

--Subbu

Posted by jtownsen on 08-Jul-2008 10:19

Posted by Admin on 08-Jul-2008 10:23

Posted by jtownsen on 09-Jul-2008 01:09

I came across a document called Messaging Fundamentals for Sonic ESB, which covers some of these topics and more. It's definitely worth reading. You can download it here: http://www.psdn.com/library/entry.jspa?externalID=2143

This thread is closed