Let us now create a new way to invoke our service. Let us use the ServiceInvoker
. Follow these steps:
BadAction
from our service list:BodyPrinter
.imports
statements:import org.jboss.soa.esb.message.Message; import org.jboss.soa.esb.message.format.MessageFactory; import org.jboss.soa.esb.client.ServiceInvoker;
throws Exception
to the static main
method:public static void main(String args[]) throws Exception
main
method:System.setProperty("javax.xml.registry.ConnectionFactoryClass","org.apache.ws.scout.registry.ConnectionFactoryImpl"); Message esbMessage = MessageFactory.getInstance().getMessage(); esbMessage.getBody().add("Chapter 3 says Hello via ServiceInvoker!"); new ServiceInvoker("Chapter3Sample","Chapter3Service").deliverAsync(esbMessage);
jboss-esb.xml
file, click Save.src
folder, expand it till the SendEsbMessage.java
file is displayed in the tree. Now click Run, select Run As | Java Application.The following message will be printed in the console
INFO [STDOUT] ************************************************** INFO [STDOUT] Body: Chapter 3 says Hello via ServiceInvoker! INFO [STDOUT] **************************************************
You created a new file (SendEsbMessage.java
) and added a few lines of code and voila, we were able to send an ESB message to the bus targeting our service. How did this work? The underlying mechanism is hidden by the ServiceInvoker
. The ServiceInvoker
uses the jbossesb-properties.xml
file found under the root of our application project. This file contains all needed configurations for the ServiceInvoker
to read and query the registry. Have a brief look at this file in JBoss Developer Studio before proceeding further.
Go ahead and modify the MEP for our service as RequestResponse
. You will need the following modifications to SendEsbMessage
:
Message response = new ServiceInvoker("Chapter3Sample","Chapter3Service").deliverSync(esbMessage, 5000); System.out.println(response.getBody().get());
You will also need to add a reply queue to the jbm-queue-service.xml
file:
<mbean code="org.jboss.jms.server.destination.QueueService" name="jboss.esb.book.samples.destination:service=Queue,name=chapter3_Request_esb_reply" xmbean-dd="xmdesc/Queue-xmbean.xml"> <depends optional-attribute- name="ServerPeer"> jboss.messaging:service=ServerPeer </depends> <depends>jboss.messaging:service=PostOffice</depends> </mbean>
Update the MyAction
class to modify the payload on the return message. See how the application behaves.