Thursday, December 18, 2008

Running Axis WSDL2Java on HTTPS WSDL

If you are getting SSL related exception while using WSDL2Java on a HTTPS WSDL (URL with HTTPS protocol) like “javax.net.ssl.SSLHandshakeException: … unable to find valid certification path to requested target”, you have to add the certificate of the host in your URL to the java’s keystore.

The above exception might occur only when you use the WSDL url (eg. https://myhost.com:443/TestWS/Test.wsdl) to generate the java files using Axis WSDL2Java ant tasks. Easiest option would be to download the WSDL on to your local file system and using it to generate java files using WSDL2Java. However, to run the client in standalone mode, you still need to add the certificate to the JRE’s keystore.

Follow the steps given above to import the certificate onto the JRE's keystore. First, we need to export the certificate from the browser to a file.

If you are using IE, here is how you can do so:
  1. Access URL with https protocol (example: service WSDL location (Ex. https://foo.com/bar?wsdl) on the browser.
  2. Internet explorer will prompt a security alert. Select the "View Certificate" button.
  3. Navigate to the tab "Details". Select the "Copy to File.." button.
  4. Certificate Export Wizard will be displayed. Select "NEXT" button.
  5. The option "DER encoded binary X.509 (.CER) will be by default selected. Select "Next".
  6. Place the file where it suits you better.
  7. Select "Next". The "Completing Certificate Export Wizard" will be displayed. Select "Finish". The will be a pop-up saying "The export was successful".
If you are using Mozilla Firefox,
  1. Open the URL in the browser. Click on the lock icon on the bottom right hand side corner of statusbar of browser window.
  2. Click on view certificate. Go to ‘Details’ tab in opened dialog.
  3. Click on Export button to save the file.
  4. Place the file where it suits you better.

Following are the steps to import the certificate signature to JRE keystore:
  1. Move the certificate file to your %JAVA_HOME%/jre/lib/security folder.
  2. In ant you can use command ant –diagnostics to find out java installation folder by using java.home system property.
  3. Make a backup copy of file named “cacerts” (the keystore) which is under %JAVA_HOME%/jre/lib/security.
  4. Open a command prompt and change directory (cd) to %JAVA_HOME%/jre/lib/security.
  5. Run following command:
  6. keytool -importcert -trustcacerts -keystore cacerts -storepass changeit -alias "<aliasname>" –file <cert file>
  7. Type in ‘yes’. (prompt appears for untrusted certificates)
  8. Run following command to verify:
  9. keytool -keystore cacerts -storepass changeit -list -alias "<aliasname>"

After importing certificate to keystore, if you are getting the exception with message “CertificateException: No name matching
<host name> found” when you are running Axis WSDL2Java ant task, make sure that you give the same alias name as the common name (CN) of the certificate while adding the cert to keystore. Also make sure that you have the WSLD URLs host name maches certificate’s CN. In this case, Certificate CN, Alias and the hostname in WSDL should all match.
To know the CN of certificate, see details of certificate in browser or use keytool –list command given int step 9 above. The text in the first line before the first comma in the output of keytool –list command is the CN.

You can delete certificates added to keystore using keytool –deletecert command and add them back with alias as CN of certificate.
Sample command to delete certificate from keystore:
JAVA_HOME\jre\lib\security> keytool -delete -keystore cacerts -storepass changeit -alias "<aliasname>"

Sunday, February 17, 2008

Integrating WebSphere MQ 6 with BEA Weblogic Server 10 using Weblogic Server's Foreign (JMS) Server feature.

This article (in fact you may call this a set of instructions) shows you how to configure Foreign (JMS) Server in BEA WebLogic 10 Server to receive/send messages from/to queues in WebSphere MQ.

What we need:
You must have following software installed in your system:
  1. WebSphere MQ 6
  2. BEA Weblogic 10
  3. JDK 1.5 or above. (This comes bundled with Weblogic)
I assume you know compile and deploy message driven beans in Weblogic.

What we going to do:
  1. Create a queue manager and two local queues namely MQSenderQueue and MQReceiverQueue in Websphere MQ, one for sending a message to our MDB at Weblogic and other one for receiving messages from Weblogic. Create and configure JMS Foreign Server, Destinations and Connection Factories.
  2. Code and deploy a MDB in Weblogic and automatically receive messages from MQ MQSenderQueue and place it at a second MQ queue MQReceiverQueue using Weblogic Foreign JSM Server features.
For testing:
  1. Put a test message to MQSenderQueue at MQ.
  2. Monitor Weblogic console to see if it has received and sent the message again to MQ.
  3. See the message received at the MQReceiverQueue at MQ.
Creating queue managers, queues and connection factories in WebSphere MQ:

Creating queue manage
Click on Start -> Program Files -> IBM WebSphere MQ -> WebSphere MQ Explorer to open WebSphere MQ Explorer. Optionally execute C:\Program Files\ibm\WebSphere MQ\bin strmqcfg.exe to run explorer.

Image #1: WebSphere MQ Explorer

On the left hand side WebSphere MQ Explorer – Navigator pane, click on 'IBM WebSphere MQ' node to expand that, if it is not expanded already.
  • To create a queue manager, right click on the ‘Queue Managers’ node and select New -> Queue Manager.
  • In the opened window, enter name “MQTest” in the name textbox and keep clicking Next-> and finally click Finish.
Now you have created a MQ queue manager namely “MQTest”. A node “MQTest” must appear under node “Queue Managers” now as shown below:


Image #2: Create queue manager in MQ


Image #3: Create queue manager in MQ


Image #4: View queue manager in MQ

Creating WebSphere MQ local queues
Now let us create MQ queues. As I mentioned above, we are going to create two queues. We will be using one of them to put a test message which is to be received by our MDB at BEA Weblogic and the other one is used by our MDB to put message the message back into second MQ queue, so that we can see the same message again in MQ, but in the second MQ queue.

We are going to create two local queues using MQ explorer.

1. To create our first local MQ queue, right click on Queues node under “MQTest” queue manager. Select New -> Local Queue.


Image #5: Create local queue in MQ

2. Enter name for our first local MQ queue “MQSenderQueue” as shown below. Click “Next” and finally “Finish”. This is the queue we will be using to put our test message.


Image #6: Create local queue in MQ

3. Repeat the above two steps to create another local queue “MQReceiverQueue”. Note the naming I followed here – used a prefix MQ to avoid the confusions since we use similar JNDI names in Weblogic also.

Optionally, you may create MQ queue manager and local queues using command prompt. Open a command prompt (CMD) and CD to C:\Program Files\ibm\WebSphere MQ\bin.
  • Create the queue manager: crtmqm MQTest.
  • Start the queue manager: strmqm MQTest.
  • Create queues in the queue manager:
 runmqsc MQTest
DEFINE QLOCAL(MQSenderQueue)
DEFINE QLOCAL(MQReceiverQueue)
end
Please note that I haven’t tried this option, so I am not sure how it works.

Creating JNDI Context and Connection factories
Next, create a simple file-based JNDI context and configure the JMS objects in that JNDI namespace. These JNDI objects are used by applications running in WebSphere Application Server Community Edition to connect to the WebSphere MQ queue manager. For this exercise, WebLogic and WebSphere MQ should be on the same machine.

The setting is for file-based JNDI. Create the directory C:\MQ-JNDI before continuing with the next step. Under new folder C:\MQ-JNDI, create a file mq-jndi.properties with the following contents:

INITIAL_CONTEXT_FACTORY=com.sun.jndi.fscontext.RefFSContextFactory
PROVIDER_URL=file:/C:/MQ-JNDI
SECURITY_AUTHENTICATION=none

Next open a command prompt, cd to C:\MQ-JNDI, create the QueueConnectionFactory and Queue objects by executing the command:

“C:\Program Files\IBM\WebSphere MQ\Java\bin\JMSAdmin.bat” -cfg mq-jndi.properties


You should see this prompt, where you can configure the JNDI objects:

InitCtx>

At the prompt, copy and paste all five lines of following commands together and press Enter.

def xaqcf(MQSenderQCF) qmgr(MQTest)
def xaqcf(MQReceiverQCF) qmgr(MQTest)
def q(MQSenderQueue) qmgr(MQTest) queue(MQSenderQueue)
def q(MQReceiverQueue) qmgr(MQTest) queue(MQReceiverQueue)
end

You should see a message like “Stopping Websphere MQ classes for Java(tm) Message Service Administration” after you press Enter after the last line “end”.
With that, we now have MQ queues ready (see following image). Next task is to create a MDB in EJB 3 and deploy it in BEA Weblogic 10.


Image #7: MQ explorer showing queue manager and local queues.

Configuring Foreign JSM Server in BEA Weblogic Server:
Before starting your Weblogic Server you have to ensure that certain MQ related jar files are in your Weblogic Server’s classpath. Easiest way to do this is to copy following jars from “C:\Program Files\ibm\WebSphere MQ\Java\lib” folder to your Weblogic domain’s lib (C:\bea\user_projects\domains\RnD\lib in my case) folder, from where Weblogic loads them up while starting.
  1. com.ibm.mq.jar
  2. com.ibm.mqjms.jar
  3. commonservices.jar
  4. dhbcore.jar
  5. fscontext.jar
Create JMS Module
Once jars are in Weblogic Server’s class path, start your Weblogic server and login to Weblogic admin console. To start with, click ‘Lock & Edit’ button.
Expand Services -> Messaging on LHS menu and click JMS Modules link.
Click on “New” button on RHS


Image #8: Configuring JMS module

Type in a name “MQIntegrationTest” in the Name text box and press Next.


Image #9: Configuring JMS module

Select the Target server (in my case it was AdminServer) as shown below, press Next and finally Finish. Click on “Activate Changes” button on LHS.


Image #10: Creating JMS module

Create JMS Foreign Server
Click on Lock & Edit again.
Select Services -> Messaging -> JMS Modules as you did earlier, and click on newly created module
“MQIntegrationTest”. Click New button.
Select “Foreign Server” radio button and click “Next”.


Image #11: Creating JMS foreign server

Enter “MQTestForeignServer” in the Name text box, click Next button. In this page you can see that the target server is selected already. Click Finish.


Image #12: Creating JMS foreign server


Image #13: Creating JMS foreign server

Activate changes by clicking “Activate Changes” button.

Click Lock & Edit again.
Open the newly created Foreign JMS server byt clicking Services -> Messaging -> JMS Modules -> MQIntegrationTest -> MQTestForeignServer.

Under the General tab under Configuration, type in following details:

JNDI Initial Context Factory:
com.sun.jndi.fscontext.RefFSContextFactory
JNDI Connection URL:
file:/C:/MQ-JNDI

Click “Save”.


Image #14: Configure JMS foreign server

Creating Destinations
Click on Destinations link in the Configuration tab at the top and click New


Image #15: Create Destinations

Type in following:

Name:
ReceiverDetails
Local JNDI Name:
jms/WLReceiverQueue
Remote JNDI Name:
MQSenderQueue

Note that the Remote JNDI Name must match the name of the queue you created in WebSphere MQ broker.
Click OK.


Image #16: Configure Destination

Repeat the above to create another Destination with following details:

Name:
ReplyDetails
Local JNDI Name:
jms/WLReplyQueue
Remote JNDI Name:
MQReceiverQueue

Now you can see two Destinations created:


Image #17: Newly created Destinations

Creating Destinations
Let’s create two Connection Factories for above queues.
Click on Connection Factories (next to Destinations) and click New


Image #18: Create connection factories

Enter following details in “Create a New Foreign JMS Connection Factory” page.

Name:
ReceiverCF
Local JNDI Name:
jms/WLReceiverQCF
Remote JNDI Name:
MQSenderQCF

Again, the remote JNDI name must match on you have given to MQ connection factory.
Click Ok.


Image #19: Create connection factory

Repeat the above steps to create one more connection factory for our ReplyQueue:

Name:
ReplyCF
Local JNDI Name:
jms/WLReplyQCF
Remote JNDI Name:
MQReceiverQCF

With that, you can see two Connection Factories created for our Foreign JMS Server like shown below:


Image #20: Connection factories

Click on “Activate Changes” to activate changes. With that you are done with creating and configuring Foreign JMS Server. The only thing pending before we test is to code and deploy our MDB.

Creating MDB and deploying it.
Here is the MDB code:

/*
* File: TestMDB.java
* Copyright (c) 2007-2008 ibswings.com. All rights reserved.
*/
package com.ibswings.ejb3.mdb;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.QueueConnection;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
* Class: TestMDB
*
* @author Ishwara Varnasi
* @date Feb 17, 2008
*/
@MessageDriven(name = "TestMDB", mappedName = "jms/WLReceiverQueue",
activationConfig = {@ActivationConfigProperty(
propertyName = "destinationType",
propertyValue = "javax.jms.Queue")}
)
public class TestMDB implements MessageListener {

public void onMessage(Message msg) {
System.out.println("\n=========================================");
System.out.println("=> Message received!");

if (msg instanceof TextMessage) {

try {
System.out.println("=> Message: "
+ ((TextMessage) msg).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}

// Place the message on to the reply queue (WLReplyQueue)
putMessage(msg);
System.out.println("=========================================");
}

@Resource(name = "WLReplyQueue", mappedName = "jms/WLReplyQueue")
private javax.jms.Queue queue;
@Resource(name = "WLReplyQCF", mappedName = "jms/WLReplyQCF")
private javax.jms.QueueConnectionFactory tcf;

public void putMessage(javax.jms.Message msg) {
System.out.println("=> Placing message on WLReplyQueue...");
QueueConnection queueConnection = null;
QueueSession queueSession = null;
QueueSender queueSender = null;

try {
queueConnection = tcf.createQueueConnection();
queueSession = queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
queueSender = queueSession.createSender(queue);
queueSender.send(msg);
System.out.println("=> Message sent!");

} catch (Exception e) {
System.out.println("Exception: ");
e.printStackTrace();

if (e instanceof JMSException) {
((JMSException) e).getLinkedException().printStackTrace();
}

} finally {

try {
System.out.println("=> Closing Connection");
queueSession.close();
queueConnection.close();
} catch (Exception e) {
System.out.println("Exception: ");
e.printStackTrace();

if (e instanceof JMSException) {
((JMSException) e).getLinkedException().printStackTrace();
}
}
}
}

@PostConstruct
public void postCreate() {
System.out.println("Bean Created");
}

@PreDestroy
public void preDestroy() {
System.out.println("Bean Destroyed");
}
}

The above code is for a simple message driven bean. It receives messages from MQSenderQueue and onMessage() prints out the content of text message received. Next it places the message back to the WLReplyQue. Note that in the code we never directly place message on to MQ queue. We are actually placing the message to a MQ queue namely MQReceiverQueue using Foreign JMS Server and local Weblogic JNDI name WLReplyQue.
Since we are using EJB 3, we actually do not need an ejb-jar.xml since we are using annotaions. However, to make it work on Weblogic 10, we still need a weblogic-ejb-jar.xml. Somehow I couldn’t get it working without this. Also note that I am using an older (90) schema for weblogic-ejb-jar.xml. Following is the content of Weblogic-ejb-jar.xml:

<weblogic-ejb-jar xmlns="http://www.bea.com/ns/weblogic/90"
j2ee="http://java.sun.com/xml/ns/j2ee"
xsi="http://www.w3.org/2001/XMLSchema-instance"
schemalocation="http://www.bea.com/ns/weblogic/90 http://www.bea.com/ns/weblogic/90/weblogic-ejb-jar.xsd">

<weblogic-enterprise-bean>
<ejb-name>TestMDB</ejb-name>
<message-driven-descriptor>
<destination-jndi-name>
jms/WLReceiverQueue
</destination-jndi-name>
<connection-factory-jndi-name>
jms/WLReceiverQCF
</connection-factory-jndi-name>
</message-driven-descriptor>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>

Create EJB jar and deploy it in Weblogic Server. You need to restart Weblogic instance if you do any changes in MQ to reflect that in Weblogic. We are all set to test now!

Testing
Open WebSphere MQ explorer, click on MQTest (our queue manager) -> Queues. On the RHS table, right click on MQSenderQueue and select “Put test message” in the popup menu. Type in a test message, say, “Hello MQ-Weblogic World!”, click “Put Message” and close.


Image #21: Put a test message for testng.
Go to your Weblogic Servers’ output console and you will see the message. Outputs look like:

=========================================
=> Message received!
=> Message: Hello MQ-Weblogic World!
=> Placing message on WLReplyQueue...

=> Message sent!
=> Closing Connection
=========================================


Now go to MQ explorer and check messages in the second queue – MQReceiverQueue – you should be able to see this message.


Image #22: Queues with messages.


This article is inspired by an article by Sanjay M Kesavan at
http://www-128.ibm.com/developerworks/websphere/library/techarticles/0604_kesavan/0604_kesavan.html
I would like thank Mr. Kesavan, author of above wonderful article.


That’s it. You may send your feedbacks to me on ivarnasi@gmail.com.