Friday, August 19, 2005

JBossMQ in JBoss

JBossMQ is the JMS 1.1 spec compliant provider in JBoss 4.x

Adrian points to a discussion on MDB singleton semantics:
MDB Singleton Semantics
(Working with NonJBoss deployments)

Sonic MQ is an industry leading vendor on messaging. They have a tutorial to integrate SonicMQ 6.1 to use with MDBs running on JBoss 3.2.5. The link is:
SonicMQ integration with JBoss

Now an user has reported a workaround to make the tutorial work on JBoss 4.0.1 onwards.

1)Edit the sonicmq-service.xml file in the deploy directory and
2) change org.jboss.jms.jndi.JBossMQProvider to
3) Restart JBoss and the example MDBs will work as advertised.

A great resource to look at configuring MDBs deployed on the JBoss platform to talk to remote queues is.

Case for J2EE Web Services with JBoss(Part 2)

This will be the placeholder to discuss J2EE Web Services with JBoss. Part 2.

Friday, August 12, 2005

Case for J2EE Web Services with JBoss(Part 1)

Importance of Web Services

Web Services have an important role to play in an enterprise. They can act as the glue between disparate systems, many of them written in different languages. The basis of integration can be:
a) Through files written on a common shared server.
b) Through a database that is shared between systems.
c) Sending messages between systems.
d) Using a common format(XML) on an universally accepted transport protocol (HTTP).

Section d) is what constitutes the world of Web Services.

The key terms to note in Web Services are as follows:
a) SOAP: It is the protocol used to exchange standard xml messages.
b) WSDL: This is the universally accepted contract for web services. Basically describes the service, the operations it supports, the message formats and the data types.
c) UDDI: It is a registry that acts as a yellow page for service discovery.
d) Marshalling: Convert Java Objects to XML Messages.
e) UnMarshalling: Convert XML Messages to Java Objects.

J2EE Web Services

Now given the basic stuff, lets move on to the J2EE paradigm of developing Web Services. The J2EE world defines a mechanism to use objects, classes and xml deployment descriptors to define web services.

Now lets discuss developing J2EE Web Services with JBoss, a robust J2EE Application Server which provides J2EE 1.4 compliant Web Services since v4.0.x

As a service provider, you will either want to expose a POJO or a SLSB. Now, these two are implementation classes, right? that contain the Web Service. Given this, the client (whether remote or local to the J2EE container) needs to see an INTERFACE.

J2EE 1.4 Web Service aptly defines an interface called SERVICE ENDPOINT INTERFACE (SEI) which basically extends java.rmi.Remote and all its methods have to throw RemoteException in addition to application exceptions. Now this SEI should front the Web Service implementation. J2EE 1.4 defines a deployment archive for the webservices - POJO is deployed as a war (Web Archive) and a SLSB is deployed as a EJB jar.

JBoss and J2EE Web Services

JBoss 4.x supports J2EE 1.4 Web Services. It supports rpc/literal and document/literal style web services only in line with WS-I Basic Profile 1.0 compliance.

Now lets take a very simple example of rpc based web services.

Lets say I have the following Web Service implementation as a POJO:

public class MyTestService
public void print(String name)
System.out.println("Hello "+name);

Okay, this is a simple example. Now this is our implementation class. If you want to deploy this as a J2EE Web Service, u will need to define a SEI. It is shown below:

public interface MyInterface extends Remote
public void print(String name) throws RemoteException;

Serverside Deployment

Now use wscompile tool from Sun's JWSDP and generate the serverside artifacts using this SEI. The artifacts will be a WSDL file, a jaxrpc mapping file. Ignore all the other stuff like serializers, stuns, ties etc. If there are any user defined types that are used in the SEI (remember this was just a simple example), then you need those.

Lets look at the wscompile configuration file(MyService-config.xml) for the serverside generation.

<configuration xmlns="">
<service name="MyService"
<interface name="org.jboss.test.MyInterface ">

The command line usage of wscompile is shown below:

wscompile.bat -cp classes -d build -gen:server -keep -mapping
mapping/jaxrpc-mapping.xml config/MyService-config.xml

Plus, you will need to hand edit a webservices.xml file that lists the SEI and a servlet-link. Plus create a web.xml file, just the way you do web programming. The servlet name will be the same as the servlet-link in the webservices.xml file. Also the servlet class will be the webservices implementation class, in our case, a POJO or MyTestService.

Serverside Packaging
Now package the war file with the implementation class, the SEI class, weservices.xml file, web.xml file and the WSDL file (Remember that it has to be RPC/Literal in our case. If it is RPC/Encoded, u might as well take a hike).
Thats it- that constitutes the serverside deployment.

Lets look at how my serverside deployment archive looks like (as we are exposing a POJO, we will be using a war file).

$ jar tvf MyTest.war
0 Tue Aug 16 01:51:08 CDT 2005 META-INF/
106 Tue Aug 16 01:51:06 CDT 2005 META-INF/MANIFEST.MF
0 Tue Aug 16 01:51:08 CDT 2005 WEB-INF/
0 Tue Aug 16 01:51:08 CDT 2005 WEB-INF/classes/
0 Tue Aug 16 01:10:36 CDT 2005 WEB-INF/classes/org/
0 Tue Aug 16 01:10:36 CDT 2005 WEB-INF/classes/org/jboss/
0 Tue Aug 16 01:50:58 CDT 2005 WEB-INF/classes/org/jboss/test/
237 Tue Aug 16 01:31:20 CDT 2005 WEB-INF/classes/org/jboss/test/MyServiceInterface.class
405 Tue Aug 16 01:51:02 CDT 2005 WEB-INF/classes/org/jboss/test/MyTestService.class
1734 Mon Aug 15 23:15:42 CDT 2005 WEB-INF/jaxrpc-mapping.xml
857 Tue Aug 16 01:29:12 CDT 2005 WEB-INF/webservices.xml
0 Mon Aug 15 23:12:10 CDT 2005 WEB-INF/wsdl/
1378 Mon Aug 15 23:12:10 CDT 2005 WEB-INF/wsdl/MyService.wsdl
393 Tue Aug 16 01:42:48 CDT 2005 WEB-INF/web.xml

Please make a good note of the file structure. Also note that MyTestService.class is my implementation of the service endpoint interface.

Want to have a look at the webservices.xml file, I hand edited?

<?xml version="1.0" encoding="UTF-8" ?>
<webservices xmlns="" xmlns:impl="http://org.jbos
ss.test" xmlns:xsi="" xsi:schemaLocatio
ervices_1_1.xsd" version="1.1">

Client Side Deployment

Still in Jboss deploy directory
We are not done yet. We need to do a client side deployment on JBoss so that javax.xml.rpc.Service object gets bound to JNDI for use by clients using the Dynamic Proxy approach. For more information on Dynamic Proxy introduced since JDK 1.3, I suggest looking for documentation on java.lang.reflect.Proxy and the invocation handlers.

With JBoss, client can either use Dynamic Proxy approach or the Dynamic Invocation Interface (DII) approach. Dynamic Proxy approach is recommended for the most compatibility. If you need to use Generated stubs/ties, sorry, using the wrong application server. Read the paragraph carefully. Only Dynamic Proxy or DII. Lets stick to Dynamic Proxy approach.

Client Side Deployment

Now when u deploy the serverside deployment on JBoss, the wsdl will be published to the context provided by the war. In our example,
http://localhost:8080/MyTest?wsdl that will give you the wsdl. Note that I would have packaged my was as MyTest.war

Now use wscompile to do client side generation using the wsdl location I just mentioned or having a physical copy of the wsdl on disk. Now out of the generated artifacts, choose:
a) Generated Service class.
b) SEI class.
c) Generated User types.
d) wsdl file
e) j2ee client file called application-client.xml that contains the service-ref element.

Lets look at the client side configuration file(MyServiceClient-config.xml) for wscompile:

<configuration xmlns="">
<wsdl location="build/MyService.wsdl" packagename="org.jboss.test">

Lets look at the command line usage of wscompile to generate client side artifacts

wscompile.bat -cp classes -d build -gen:client -keep -mapping
mapping/jaxrpc-client-mapping.xml config/MyServiceClient-config.xml

Package this together as a jar file and deploy on JBoss. This will bind a Service Proxy to JNDI to be downloadable by WS clients.

Lets have a look at my client side jar that needs to be deployed in the deploy directory of JBoss.

$ jar tvf MyTest-client.jar
0 Tue Aug 16 01:51:08 CDT 2005 META-INF/
106 Tue Aug 16 01:51:06 CDT 2005 META-INF/MANIFEST.MF
797 Tue Aug 16 01:19:58 CDT 2005 META-INF/application-client.xml
242 Tue Aug 16 01:19:58 CDT 2005 META-INF/jboss-client.xml
1602 Tue Aug 16 01:19:58 CDT 2005 META-INF/jaxrpc-client-mapping.xml
0 Tue Aug 16 01:37:48 CDT 2005 META-INF/wsdl/
1382 Tue Aug 16 01:37:48 CDT 2005 META-INF/wsdl/MyService.wsdl
0 Tue Aug 16 01:51:08 CDT 2005 My.jar

Note My.jar contains the Service class wscompile generates. I have attached the structure below:

$ jar tvf My.jar
0 Tue Aug 16 01:51:08 CDT 2005 org/
106 Tue Aug 16 01:51:06 CDT 2005 org/jboss
797 Tue Aug 16 01:19:58 CDT 2005 org/jboss/test
242 Tue Aug 16 01:19:58 CDT 2005 org/jboss/test/MyServiceInterface.class
1602 Tue Aug 16 01:19:58 CDT 2005 org/jboss/test/MyService.class

As shown you will still need to package the service endpoint interface along with the Service interface generated by wscompile.

Now let me show you application-client.xml that is packaged.

<?xml version="1.0" encoding="UTF-8"?>
<application-client xmlns=""
<display-name>webservice client</display-name>

Lets look at the jboss-client.xml that is packaged.

<?xml version='1.0' encoding='UTF-8' ?>

<!DOCTYPE jboss-client PUBLIC
"-//JBoss//DTD Application Client 3.2//EN"


Note jboss-client.xml is very important as it identifies the client context under which the JNDI bindings will happen.

Web Services Consumers

import javax.xml.namespace.*;
import javax.xml.rpc.*;
import javax.naming.*;
import java.util.*;

import org.jboss.test.*;

public class TestClient
private static final String WSDL_LOCATION = "http://localhost:8080/MyTest?wsdl"
private static String NAMESPACE = "";
private static final QName SERVICE_NAME = new QName(NAMESPACE, "MyService");
private static String NS_XSD = "";

public static void main(String[] args) throws Exception
InitialContext iniCtx = getInitialContext();
Service service = (Service)iniCtx.lookup("java:comp/env/service/MyService");
MyServiceInterface endpoint =
//Invoke the Web Service

* Get the client's env context
protected static InitialContext getInitialContext() throws NamingException
Properties env = new Properties();
env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming.client");
env.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099");
env.setProperty("j2ee.clientName", "ws-client");
return new InitialContext(env);

Notice the j2ee.clientName that uses the value from the jboss-client.xml file that was packaged in the client deployment jar. As you can see the test client code uses Dynamic Proxy approach.

Thats it for rpc literal webservices. For more information, please refer to the JBossWS Wiki page in the resources section. Also there are a lot of testcases/samples in the testsuite module of the JBoss CVS source repostitory.

XML Data Binding Resources
Accessing Web Services from J2EE Components
JBoss Web Services

General Articles on the Web
Consuming a webservice using JAX-RPC
Web Services with JAX-RPC
Apache Axis 1.2 User Guide
The Argument Against SOAP Encoding

Frequent Questions:
- How Do I get the ServletRequest and associated certificated from a WS Invocation
in JBoss Web Services?

MessageContext messageContext = this.ctx.getMessageContext();
HttpServletRequest request = (HttpServletRequest) messageContext.getProperty("transport.http.servletRequest");
X509Certificate[] certificates = (X509Certificate[]) request.getAttribute("javax.servlet.request.X509Certificate");

Sunday, August 07, 2005

Open Source Jaxr implementation (Apache Scout)

I would like you to try out Apache Scout, the first open source implementation of the Java API for XML Registries (JAXR). Why am I preaching it? I wrote it.

JAXR is a specification that is mandated by J2EE 1.4 specification. I integrated Apache Scout (JAXR implementation) and Apache jUDDI (UDDI v2.0 compliant) into JBoss, since v4.0.2

So if you are in need of a UDDI registry and a standard way of accessing it, please try out JBoss v4.0.2 (or later).
JAXR in JBoss