Saturday, November 12, 2011

Direct Asynchronous & Synchronous Invocation of ESB Services

Directly sending ESB messages to arbitrary channels is an invaluable service to debug ESB projects. Assuming that you are using JBoss Developer Studio, this requires the following steps:

1 – Create an ESB project. We will never deploy this project, but we need to have an immense list of jars and this is perhaps the simplest way of getting that list ready. Your final project will look like this:


2 - For now, add the following class to the source of your project (you can find it in the helloworld sample of the JBossESB 4.10 server):


/*
* JBoss, Home of Professional Open Source
* Copyright 2006, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/



import org.jboss.soa.esb.message.Message;
import org.jboss.soa.esb.message.format.MessageFactory;
import org.jboss.soa.esb.client.ServiceInvoker;

/**
* Standalone class with to send ESB messages to a 'known' [category,name].
* <p/> arg0 - service category
* <br/>arg1 - service name
* <br/>arg2 - Text of message to send
*
* @author <a href="mailto:schifest@heuristica.com.ar">schifest@heuristica.com.ar</a>
* @since Version 4.0
*
*/
public class SendEsbMessage
{
public static void main(String args[]) throws Exception
{
// Setting the ConnectionFactory such that it will use scout
System.setProperty("javax.xml.registry.ConnectionFactoryClass","org.apache.ws.scout.registry.ConnectionFactoryImpl");

if (args.length < 3)
{
System.out.println("Usage SendEsbMessage <category> <name> <text to send>");
}

Message esbMessage = MessageFactory.getInstance().getMessage();
esbMessage.getBody().add(args[2]);
new ServiceInvoker(args[0], args[1]).deliverAsync(esbMessage);
}
}


What if you needed a synchronous call instead of an asynchronous one? You could do like this with the ServiceInvoker object:



ServiceInvoker si = new ServiceInvoker(args[0], args[1]);
Message reply = si.deliverSync(esbMessage, 60000);



The 60000 is the number of milliseconds the invoker will wait for a reply before trying again.




3 - To run this file, you need to create a run configuration, to define the execution arguments, as we show in the following figure:

However, if we run this class now we will get an exception. We still need to add a few configurations.

4 - First, we need to have a jbossesb-properties.xml file in the classpath (e.g., you can create a source folder named “conf” to serve that purpose). You can find one properties file like this in the samples of the JBossESB server. For convenience we reproduce the file here:


<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- JBoss, Home of Professional Open Source Copyright 2006, JBoss Inc.,
and others contributors as indicated by the @authors tag. All rights reserved.
See the copyright.txt in the distribution for a full listing of individual
contributors. This copyrighted material is made available to anyone wishing
to use, modify, copy, or redistribute it subject to the terms and conditions
of the GNU Lesser General Public License, v. 2.1. This program is distributed
in the hope that it will be useful, but WITHOUT A WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details. You should have
received a copy of the GNU Lesser General Public License, v.2.1 along with
this distribution; if not, write to the Free Software Foundation, Inc., 51
Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. (C) 2005-2006,
@author JBoss Inc. -->
<!-- $Id: jbossesb-unittest-properties.xml $ -->
<!-- These options are described in the JBossESB manual. Defaults are provided
here for convenience only. Please read through this file prior to using the
system, and consider updating the specified entries. -->
<esb xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="jbossesb-1_0.xsd">
<properties name="core">
<property name="org.jboss.soa.esb.jndi.server.type" value="jboss" />
<property name="org.jboss.soa.esb.jndi.server.url" value="localhost" />
<property name="org.jboss.soa.esb.persistence.connection.factory"
value="org.jboss.internal.soa.esb.persistence.format.MessageStoreFactoryImpl" />
<property name="jboss.esb.invm.scope.default" value="NONE" />
</properties>
<properties name="registry">
<property name="org.jboss.soa.esb.registry.queryManagerURI"
value="org.apache.juddi.v3.client.transport.wrapper.UDDIInquiryService#inquire" />
<property name="org.jboss.soa.esb.registry.lifeCycleManagerURI"
value="org.apache.juddi.v3.client.transport.wrapper.UDDIPublicationService#publish" />
<property name="org.jboss.soa.esb.registry.securityManagerURI"
value="org.apache.juddi.v3.client.transport.wrapper.UDDISecurityService#secure" />
<property name="org.jboss.soa.esb.registry.implementationClass"
value="org.jboss.internal.soa.esb.services.registry.JAXRRegistryImpl" />
<property name="org.jboss.soa.esb.registry.factoryClass"
value="org.apache.ws.scout.registry.ConnectionFactoryImpl" />
<property name="org.jboss.soa.esb.registry.user" value="root" />
<property name="org.jboss.soa.esb.registry.password" value="root" />
<!-- the following parameter is scout specific to set the type of communication
between scout and the UDDI (embedded, rmi, soap) -->
<property name="org.jboss.soa.esb.scout.proxy.transportClass"
value="org.apache.ws.scout.transport.LocalTransport" />
<property name="org.jboss.soa.esb.scout.proxy.uddiVersion"
value="3.0" />
<property name="org.jboss.soa.esb.scout.proxy.uddiNameSpace"
value="urn:uddi-org:api_v3" />
<!-- Organization Category to be used by this deployment. -->
<property name="org.jboss.soa.esb.registry.orgCategory"
value="org.jboss.soa.esb.:category" />
</properties>
<properties name="transports" depends="core">
<property name="org.jboss.soa.esb.mail.smtp.host" value="localhost" />
<property name="org.jboss.soa.esb.mail.smtp.user" value="jbossesb" />
<property name="org.jboss.soa.esb.mail.smtp.password" value="" />
<property name="org.jboss.soa.esb.mail.smtp.port" value="25" />
</properties>
<properties name="connection">
<property name="min-pool-size" value="5" />
<property name="max-pool-size" value="10" />
<property name="blocking-timeout-millis" value="5000" />
<property name="abandoned-connection-timeout" value="10000" />
<property name="abandoned-connection-time-interval" value="30000" />
</properties>
<properties name="dbstore">
<property name="org.jboss.soa.esb.persistence.db.connection.url"
value="jdbc:hsqldb:hsql://localhost:9001/" />
<property name="org.jboss.soa.esb.persistence.db.jdbc.driver"
value="org.hsqldb.jdbcDriver" />
<property name="org.jboss.soa.esb.persistence.db.user" value="sa" />
<property name="org.jboss.soa.esb.persistence.db.pwd" value="" />
<property name="org.jboss.soa.esb.persistence.db.pool.initial.size"
value="2" />
<property name="org.jboss.soa.esb.persistence.db.pool.min.size"
value="2" />
<property name="org.jboss.soa.esb.persistence.db.pool.max.size"
value="5" />
<!--table managed by pool to test for valid connections - created by pool
automatically -->
<property name="org.jboss.soa.esb.persistence.db.pool.test.table"
value="pooltest" />
<!-- # of milliseconds to timeout waiting for a connection from pool -->
<property name="org.jboss.soa.esb.persistence.db.pool.timeout.millis"
value="5000" />
<property name="org.jboss.soa.esb.persistence.db.conn.manager"
value="org.jboss.internal.soa.esb.persistence.manager.StandaloneConnectionManager" />
</properties>
<properties name="messagerouting">
<property name="org.jboss.soa.esb.routing.cbrClass"
value="org.jboss.internal.soa.esb.services.routing.cbr.JBossRulesRouter" />
</properties>
</esb>


5 - Finally, an uddi.xml file inside the conf/META-INF directory will do the trick. You can find this file in the conf directory of the samples, but, again, we reproduce a short version here:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<uddi>
<reloadDelay>5000</reloadDelay>
<manager name="test-manager">
<nodes>
<node>
<!-- required 'default' node -->
<name>default</name>
<description>Main jUDDI node</description>
<properties>
<property name="serverName" value="localhost" />
<property name="serverPort" value="8880" />
</properties>
<!-- RMI Transport Settings -->
<proxyTransport>org.jboss.internal.soa.esb.registry.client.JuddiRMITransport
</proxyTransport>
<custodyTransferUrl>/juddiv3/UDDICustodyTransferService
</custodyTransferUrl>
<inquiryUrl>/juddiv3/UDDIInquiryService</inquiryUrl>
<publishUrl>/juddiv3/UDDIPublicationService</publishUrl>
<securityUrl>/juddiv3/UDDISecurityService</securityUrl>
<subscriptionUrl>/juddiv3/UDDISubscriptionService</subscriptionUrl>
<subscriptionListenerUrl>/juddiv3/UDDISubscriptionListenerService
</subscriptionListenerUrl>
<juddiApiUrl>/juddiv3/JUDDIApiService</juddiApiUrl>
<javaNamingFactoryInitial>org.jnp.interfaces.NamingContextFactory
</javaNamingFactoryInitial>
<javaNamingFactoryUrlPkgs>org.jboss.naming
</javaNamingFactoryUrlPkgs>
<javaNamingProviderUrl>jnp://localhost:1099</javaNamingProviderUrl>
</node>
</nodes>
</manager>
</uddi>



1 comment: