Time for action – examining exceptions

Let us now create a new way to invoke our service. Let us use the ServiceInvoker. Follow these steps:

  1. Remove BadAction from our service list:
    Time for action – examining exceptions
  2. Click OK. Ensure that we have only one action, BodyPrinter.
  3. Create a new class. Enter Name as "SendEsbMessage", Package as "org.jboss.soa.esb.samples.chapter3.test", check the box before public static void main(String[] args):
    Time for action – examining exceptions
  4. Click Finish.
  5. Add the following imports statements:
    import org.jboss.soa.esb.message.Message;
    import org.jboss.soa.esb.message.format.MessageFactory;
    import org.jboss.soa.esb.client.ServiceInvoker;
  6. Add throws Exception to the static main method:
    public static void main(String args[]) throws Exception
  7. Add the following to the 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);
  8. Select the jboss-esb.xml file, click Save.
  9. Deploy the application using the Run menu and select Run As | Run on Server.
  10. Select the 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] **************************************************
    

What just happened?

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.

Have a go hero – experimenting with MEPs and sync delivery

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset