Webservices Archive

JMS Based Webservice on Weblogic Server

By default, client applications use HTTP/S as the connection protocol when invoking a WebLogic Web Service. You can, however, configure a WebLogic Web Service so that client applications can also use JMS as the transport when invoking the Web Service.When a WebLogic Web Service is configured to use JMS as the connection transport:The generated WSDL of the Web Service contains two port definitions: one with an HTTP/S binding and one with a JMS binding. When you invoke the Web Service in your client application, you can choose which port, and thus which type of transport, you want to use.Warning:     Non-WebLogic client applications, such as a .NET client, will not be able to invoke the Web Service using the JMS binding.The clientgen Ant task creates a Service implementation that contains two getPortXXX() methods, one for HTTP/S and one for JMS.Note:    You can configure any WebLogic Web Service to include a JMS binding in its WSDL. This feature is independent of JMS-implemented WebLogic Web Services.

Read more

Step1 :-

Create JMS Server and target it to the AdminServer running on http://localhost:7001Create a ConnectionFactory and a Queue targeted to the JMSServer.For Screenshots follow this article.
Step2 :- Create a JWS File

package demo;
import weblogic.jws.*;

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.soap.SOAPBinding;

@WebService(name=”HelloWorldPortType”,serviceName=”HelloWorldService”,      targetNamespace=”http://www.bea.com”)
@SOAPBinding(style=SOAPBinding.Style.DOCUMENT,use=SOAPBinding.Use.LITERAL,           parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)

@WLJmsTransport(serviceUri=”HelloWorldService”,portName=”HelloWorldServicePort”,                    contextPath=”HelloWorldService”,connectionFactory=”ConnectionFactory-0″,queue=”Queue-0″)

public class HelloWorldImpl {
@WebMethod()
public String sayHello(String s) {
return “Hello ” + s;
}
}

Step 3:- Create a Client

package demo;
import demo.HelloWorldService;
import demo.HelloWorldService_Impl;
import demo.HelloWorldPortType;
import weblogic.wsee.jaxrpc.WLStub;
import java.util.ArrayList;
import java.util.List;
import javax.xml.rpc.Stub;

public class HelloWorldClient
{
public static void main(String[] args) throws Throwable {

String wsdl = args[0];
HelloWorldService service = new HelloWorldService_Impl(wsdl);
HelloWorldPortType port = service.getHelloWorldServicePort();
Stub stub = (Stub)port;
stub._setProperty(WLStub.JMS_TRANSPORT_JNDI_URL, “t3://localhost:7001”);
String response = port.sayHello(“World”);
System.out.println(“response = ” + response);
}
}

Step 4:- Create a build

<project name=”Sample JMS Webservice” default=”all” basedir=”.”>

<property name=”ws.file” value=”HelloWorldImpl” />
<property name=”ear.dir” value=”./ear” />
<property name=”client.dir” value=”./client” />

<path id=”client.class.path”>
<pathelement path=”${client.dir}”/>
<pathelement path=”${java.class.path}”/>
</path>

<taskdef name=”jwsc”
classname=”weblogic.wsee.tools.anttasks.JwscTask” />

<taskdef name=”clientgen”
classname=”weblogic.wsee.tools.anttasks.ClientGenTask” />

<target name=”all” depends=”clean,
server,
client,
deploy” />

<target name=”build” depends=”clean,
server,
client” />

<target name=”clean”>
<delete dir=”${ear.dir}”/>
<delete dir=”${client.dir}”/>
</target>

<target name=”server”>
<mkdir dir=”${ear.dir}”/>
<jwsc
srcdir=”${basedir}”
destdir=”${ear.dir}”
classpath=”${java.class.path}”
fork=”true”
keepGenerated=”true”
deprecation=”${deprecation}”
debug=”${debug}”
verbose=”false”>
<jws file=”${ws.file}.java” explode=”true”/>
</jwsc>
</target>

<target name=”deploy”>
<wldeploy action=”deploy”
source=”${ear.dir}” user=”weblogic”
password=”weblogic” verbose=”true”
failonerror=”${failondeploy}”
adminurl=”t3://localhost:7001″
targets=”AdminServer” />
</target>

<target name=”client”>
<mkdir dir=”${client.dir}”/>
<clientgen
wsdl=”${ear.dir}/${ws.file}/WEB-INF/HelloWorldService.wsdl”
destDir=”${client.dir}”
classpath=”${java.class.path}”
packageName=”demo”/>
<javac
srcdir=”${client.dir}” destdir=”${client.dir}”
classpath=”${java.class.path}”
includes=”demo/**/*.java”/>
<javac
srcdir=”${basedir}” destdir=”${client.dir}”
classpath=”${java.class.path};${client.dir}”
includes=”HelloWorldClient.java”/>
</target>

<target name=”run” >
<java fork=”true”
classname=”demo.HelloWorldClient”
failonerror=”true” >
<classpath refid=”client.class.path”/>
<arg line=”http://localhost:7001/HelloWorldService/HelloWorldService?WSDL” />
</java>
</target>

</project>

Step 5:- Create a folder and keep the source files and the build.xml in that directory. Also copy the setWLSEnv.cmd from WL_HOME\bin to this folder. Create two folders in this folder, ear and client.

Step 6 :- Set the Environment

D:\Replications\jmswebservice>setWLSEnv.cmd

Step 7:- Create the Webservice Ear

D:\Replications\jmswebservice>ant server
Buildfile: build.xml

server:
[jwsc] JWS: processing module /HelloWorldImpl
[jwsc] Parsing source files
[jwsc] Parsing source files
[jwsc] 1 JWS files being processed for module /HelloWorldImpl
[jwsc] JWS: D:\Replications\jmswebservice\HelloWorldImpl.java Validated.
[jwsc] Since compiler setting isn’t classic or modern,ignoring fork setting
.
[jwsc] Compiling 2 source files to C:\Users\khanf\AppData\Local\Temp\_bxr00
h
[jwsc] Since compiler setting isn’t classic or modern,ignoring fork setting
.
[jwsc] Copying 1 file to D:\Replications\jmswebservice\ear\HelloWorldImpl\W
EB-INF
[jwsc] Copying 22 files to D:\Replications\jmswebservice\ear\HelloWorldImpl
\WEB-INF
[jwsc] Copying 2 files to D:\Replications\jmswebservice\ear\HelloWorldImpl\
WEB-INF\classes
[jwsc] Copying 1 file to D:\Replications\jmswebservice\ear\HelloWorldImpl
[jwsc] [EarFile] Application File : D:\Replications\jmswebservice\ear\META-
INF\application.xml
[AntUtil.deleteDir] Deleting directory C:\Users\khanf\AppData\Local\Temp\_bxr00h

BUILD SUCCESSFUL
Total time: 16 seconds

Step 8:- Deploy the Ear

D:\Replications\jmswebservice>ant deploy
Buildfile: build.xml

deploy:
[wldeploy] weblogic.Deployer -verbose -noexit -source D:\Replications\jmswebser
vice\ear -targets AdminServer -adminurl t3://localhost:7001 -user weblogic -pass
word ******** -deploy
[wldeploy] weblogic.Deployer invoked with options: -verbose -noexit -source D:
\Replications\jmswebservice\ear -targets AdminServer -adminurl t3://localhost:70
01 -user weblogic -deploy
[wldeploy] <Apr 30, 2011 2:06:34 PM IST> <Info> <J2EE Deployment SPI> <BEA-2601
21> <Initiating deploy operation for application, ear [archive: D:\Replications\
jmswebservice\ear], to AdminServer .>
[wldeploy] Task 3 initiated: [Deployer:149026]deploy application ear on AdminSe
rver.
[wldeploy] Task 3 completed: [Deployer:149026]deploy application ear on AdminSe
rver.
[wldeploy] Target state: deploy completed on Server AdminServer
[wldeploy]
[wldeploy] Target Assignments:
[wldeploy] + ear AdminServer

BUILD SUCCESSFUL

Step 9 :- Compile the client

D:\Replications\jmswebservice>ant client
Buildfile: build.xml

client:
[clientgen] Ignoring JAX-WS options – building a JAX-RPC client
[clientgen]
[clientgen] *********** jax-rpc clientgen attribute settings ***************
[clientgen]
[clientgen] wsdlURI: file:/D:/Replications/jmswebservice/ear/HelloWorldImpl/WEB-
INF/HelloWorldService.wsdl
[clientgen] serviceName : null
[clientgen] packageName : demo
[clientgen] destDir : D:\Replications\jmswebservice\client
[clientgen] handlerChainFile : null
[clientgen] generatePolicyMethods : false
[clientgen] autoDetectWrapped : true
[clientgen] jaxRPCWrappedArrayStyle : true
[clientgen] generateAsyncMethods : true
[clientgen]
[clientgen] *********** jax-rpc clientgen attribute settings end ***************

[clientgen] Package name is demo
[clientgen] DestDir is D:\Replications\jmswebservice\client
[clientgen] class name is HelloWorldPortType_Stub
[clientgen] service class name is HelloWorldService
[clientgen] Porttype name is HelloWorldPortType
[clientgen] service impl name is HelloWorldService_Impl
[javac] Compiling 4 source files to D:\Replications\jmswebservice\client
[javac] Note: D:\Replications\jmswebservice\client\demo\HelloWorldPortType_S
tub.java uses unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] Compiling 1 source file to D:\Replications\jmswebservice\client

BUILD SUCCESSFUL

Step 10:- Run the Client

D:\Replications\jmswebservice>ant run
Buildfile: build.xml

run:
[java] response = Hello World

BUILD SUCCESSFUL
Total time: 10 seconds

If you have a look at the WSDL

http://localhost:7001/HelloWorldService/HelloWorldService?WSDL

The service will look like this.

<s0:service name=”HelloWorldService”>
<s0:port binding=”s1:HelloWorldServiceSoapBinding” name=”HelloWorldServicePort”>
<s2:address location=”jms://192.168.3.8:7001/HelloWorldService/HelloWorldService?URI=Queue-0&FACTORY=ConnectionFactory-0″ />
</s0:port>
</s0:service>

Let me know if you face any issues while running this sample.

 

Download the source here : jmswebservice

Using Canned Policy with Weblogic Server.

SimpleWS.java

package demo;

import weblogic.jws.WLHttpTransport;
import weblogic.jws.Policies;
import weblogic.jws.Policy;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.soap.SOAPBinding;

@WebService(name=”SimpleWSPortType”,
serviceName=”SimpleWSService”,
targetNamespace=”http://www.oracle.com”)

@SOAPBinding(style=SOAPBinding.Style.DOCUMENT,
use=SOAPBinding.Use.LITERAL,
parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)

@WLHttpTransport(contextPath=”SimpleWSService”,
serviceUri=”SimpleWSService”,
portName=”SimpleWSServicePort”)

@Policy(uri=”policy:Sign.xml”)

public class SimpleWS {

@WebMethod()
public String sayHello(String s) {
return “Hello ” + s;
}
}

Client.java

package demo;

import demo.*;

import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import javax.xml.rpc.Stub;

import weblogic.security.SSL.TrustManager;
import weblogic.wsee.security.bst.ClientBSTCredentialProvider;

import weblogic.xml.crypto.wss.WSSecurityContext;
import weblogic.xml.crypto.wss.provider.CredentialProvider;

public class Client {
public static void main(String[] args) throws Throwable {

//client private key file
String keyFile = args[0];

//client certificate
String clientCertFile = args[1];

String wsdl = args[2];

// Standard JAX-RPC code to get a service and port type

SimpleWSService service = new SimpleWSService_Impl(wsdl);
SimpleWSPortType port = service.getSimpleWSServicePort();

//create emtpy list of credential providers
List credProviders = new ArrayList();

//Create client-side BinarySecurityToken credential provider that uses
// X.509 for identity, based on certificate and keys parameters
CredentialProvider cp = new ClientBSTCredentialProvider(clientCertFile, keyFile);
credProviders.add(cp);

Stub stub = (Stub)port;

// Set stub property to point to list of credential providers
stub._setProperty(WSSecurityContext.CREDENTIAL_PROVIDER_LIST, credProviders);

stub._setProperty(WSSecurityContext.TRUST_MANAGER,
new TrustManager(){
public boolean certificateCallback(X509Certificate[] chain, int validateErr){
return true;
}
} );

String response = port.sayHello(“World”);
System.out.println(“response = ” + response);
}
}

build.xml

<project name=”SimpleWS” default=”all” basedir=”.”>

<property name=”ws.file” value=”SimpleWS” />
<property name=”ear.dir” value=”EAR” />
<property name=”client.dir” value=”Client-dir” />

<path id=”client.class.path”>
<pathelement path=”${client.dir}”/>
<pathelement path=”${java.class.path}”/>
</path>

<taskdef name=”jwsc”
classname=”weblogic.wsee.tools.anttasks.JwscTask” />

<taskdef name=”clientgen”
classname=”weblogic.wsee.tools.anttasks.ClientGenTask” />

<target name=”all” depends=”clean,server,client, deploy” />

<target name=”build” depends=”clean,server,client” />

<target name=”clean”>
<delete dir=”${ear.dir}”/>
<delete dir=”${client.dir}”/>
</target>

<target name=”server”>
<mkdir dir=”${ear.dir}”/>
<jwsc
srcdir=”${basedir}”
destdir=”${ear.dir}”
classpath=”${java.class.path}”
fork=”true”
keepGenerated=”true”
deprecation=”${deprecation}”
debug=”${debug}”
verbose=”false”>
<jws file=”${ws.file}.java” explode=”true”/>
</jwsc>
</target>

<target name=”deploy”>
<wldeploy action=”deploy”
source=”${ear.dir}” user=”weblogic”
password=”weblogic” verbose=”true”
failonerror=”${failondeploy}”
adminurl=”t3://localhost:7001?
targets=”AdminServer” />
</target>

<target name=”redeploy”>
<wldeploy action=”redeploy” source=”${ear.dir}”
user=”weblogic” password=”weblogic” verbose=”true”
failonerror=”${failondeploy}”
adminurl=”t3://localhost:7001?
targets=”AdminServer” />
</target>

<target name=”client”>
<mkdir dir=”${client.dir}”/>
<clientgen
wsdl=”${ear.dir}/${ws.file}/WEB-INF/SimpleWSService.wsdl”
destDir=”${client.dir}”
classpath=”${java.class.path}”
packageName=”demo”/>
<javac
srcdir=”${client.dir}” destdir=”${client.dir}”
classpath=”${java.class.path}”
includes=”demo/*.java”/>
<javac
srcdir=”${basedir}” destdir=”${client.dir}”
classpath=”${java.class.path};${client.dir}”
includes=”Client.java”/>
</target>

<target name=”run” >
<java fork=”true”
classname=”demo.Client”
failonerror=”true” >
<classpath refid=”client.class.path”/>
<arg line=”client-key-pkcs8.der client-cert.der http://localhost:7001/SimpleWSService/SimpleWSService?WSDL” />
</java>
</target>

</project>

References:

http://download-llnw.oracle.com/docs/cd/E15051_01/wls/docs103/webserv_sec/message.html