Monday, June 29, 2009

Message Driven Bean

Message Driven Bean

Message Driven Bean (MDB) is an enterprise bean which runs inside the EJB container and it acts as Listener for the JMS asynchronous message . It does not have Home and Remote interface as Session or Entity bean. It is called by container when container receives JMS asynchronous message. MDB has to implement MessageListener which has a method onMessage(Message msg). When the container calls the MDB it passes the message to onMesage() method and then MDB process that message.

Example Program:

Server Side Program

package org.jboss.tutorial.mdb.bean;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.BytesMessage;
import javax.jms.Message;
import javax.jms.MessageListener;

@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),
@ActivationConfigProperty(propertyName="destination", propertyValue="queue/tutorial/example")
})
public class ExampleMDB implements MessageListener {
public void onMessage(Message recvMsg) {
System.out.println("----------------");
System.out.println("Received message");
System.out.println("----------------");
System.out.println("Received message object: " + recvMsg);
BytesMessage byteMsg = (BytesMessage) recvMsg;
}
}

Client Side Program
package org.jboss.tutorial.mdb.client;


import java.io.FileInputStream;
import java.util.Properties;

import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.InitialContext;

public class Client {
public static void main(String[] args) throws Exception {
QueueConnection cnn = null;
QueueSender sender = null;
QueueSession session = null;

Properties props = new Properties();
props.load(new FileInputStream("EJBJNDI.prop")); //
InitialContext ic = new InitialContext(props);

//InitialContext ctx = new InitialContext();
Queue queue = (Queue) ic.lookup("queue/tutorial/example");
QueueConnectionFactory factory = (QueueConnectionFactory) ic.lookup("ConnectionFactory");
cnn = factory.createQueueConnection();
session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);

TextMessage msg = session.createTextMessage("Hello World");

sender = session.createSender(queue);
sender.send(msg);
System.out.println("Message sent successfully to remote queue.");

}
}

EJBJNDI.prop
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099

Web Service Security

Web Service Security Encryption with Rampart 1.4 and Axis2 1.4.1

Writing Client Based on the .JKS and .CER files

  • Download axis2-1.4.1-bin.zip (Standard Binary Distribution) from axis site, the URL is http://ws.apache.org/axis2/download/1_4/download.cgi
  • Download rampart-dist-1.4-bin.zip (Standard Binary Distribution) from Apache Rampart, the URL is http://ws.apache.org/rampart/download/1.4/download.cgi
  • Copy these 2 zip files in C:\ and extract it.
Creating Project in Eclipse AND CREATING CLIENT AND STUB
  • Create a new Project in C:\ using Eclipse with the name TestWSS. File --> New --> Java Project
  • Set your class path to JAVA_HOME
  • Open Command Prompt and Go to C:\axis2-1.4.1-bin\axis2-1.4.1\bin
  • Execute axis2.bat
  • Copy your wsdl file into C:\axis2-1.4.1-bin\axis2-1.4.1\bin (Assume the wsdl file name is meal.wsdl).
  • Execute the below command from command prompt
  • C:\axis2-1.4.1-bin\axis2-1.4.1\bin>wsdl2java -uri C:\axis2-1.4.1-bin\axis2-1.4.1\bin\meal.wsdl
  • After executing the above command you will get the stub and Handler class in the C:\axis2-1.4.1-bin\axis2-1.4.1\bin with package.
Placing Client Files in Project SRC and applying signature through Rampart 1.4
  • Copy generated file into C:\TestWSS\src folder.
  • Create a folder parallel to src with the name client-repo and copy module directory from C:\rampart-dist-1.4-bin\rampart-1.4\ to C:\TestWSS\client-repo.
  • This module folder should contain rampart-1.4.mar and addressing-1.41.mar
  • Add all .jar files present in the C:\rampart-dist-1.4-bin\rampart-1.4\lib directory needs to add to the Eclipse Project build path.
  • Add all .jar files present in the C:\axis2-1.4-bin\axis2-1.4\lib directory needs to add to the Eclipse Project build path.
  • Refresh the project in Eclipse and compile it this time you won’t get any compilation errors because we already set the required jars to the class path.
Writing Client Policy File
As you can see, the above security policy contains two main security assertions: an asymmetric binding assertion and a signed parts assertion. Asymmetric binding defines what keys to be used and a few additional properties such as which algorithms to be used in cryptographic operations, layout of the security header, etc. Signed parts assertion defines what parts of the message should be signed. In this tutorial we will be signing the SOAP body of the message.

Web Service Creation using tools

What are Web Services?
* Web services are application components
* Web services communicate using open protocols
* Web services are self-contained and self-describing
* Web services can be discovered using UDDI
* Web services can be used by other applications
* XML is the basis for Web services
How Does it Work?
* The basic Web services platform is XML + HTTP.
* XML provides a language which can be used between different platforms and languages and still express complex messages and functions.
* The HTTP protocol is the most used Internet protocol.
* Web services platform elements:SOAP (Simple Object Access Protocol)

* UDDI (Universal Description, Discovery and Integration)

* WSDL (Web Services Description Language)We will explain these topics later in the tutorial.

Writing Web Service

Writing a sample Web Service Application. Create POJO classes, the below are the sample Code

/////////////////////////////////////////////////////////////////////////////////


package com.marlabs.gangadhar;
public class Address {
private String street;
private String city;
private Long pin;
/**
* @return the city
*/
public String getCity() {
return city;
}
/**
* @param city the city to set
*/
public void setCity(String city) {
this.city = city;
}
/**
* @return the pin
*/
public Long getPin() {
return pin;
}
/**
* @param pin the pin to set
*/
public void setPin(Long pin) {
this.pin = pin;
}
/**
* @return the street
*/
public String getStreet() {
return street;
}
/**
* @param street the street to set
*/
public void setStreet(String street) {
this.street = street;
}
}

////////////////////////////////////////////////////////////////////////////////


package com.marlabs.gangadhar;
public class Employee {
private String name;
private String empno;
private Address address;
/**
* @return the address
*/
public Address getAddress() {
return address;
}
/**
* @param address the address to set
*/
public void setAddress(Address address) {
this.address = address;
}
/**
* @return the empno
*/
public String getEmpno() {
return empno;
}
/**
* @param empno the empno to set
*/
public void setEmpno(String empno) {
this.empno = empno;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
}

/ /////////////////////////////////////////////////////////////////////////////////

Write the web service implementation class, below is the sample code

package com.marlabs.gangadhar;

import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

import org.jboss.wsf.spi.annotation.WebContext;

@WebService(name = "Employee", targetNamespace = "http://gangadhar.marlabs.com/")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
@WebContext(contextRoot = "/operations", urlPattern = "/sendEmployeeInfo")
@Stateless()
public class EmpWebImp {
@WebMethod(operationName = "sendEmployeeInfo", action = "sendEmployeeInfo")
public void sendEmployeeInfo(@WebParam(name = "Employee", targetNamespace = "http://gangadhar.marlabs.com/", partName = "employee")
Employee employee) {
System.out.println("Employee Name: " + employee.getName());
System.out.println("Employee No : " + employee.getEmpno());
Address address = employee.getAddress();
System.out.println("Street : " + address.getStreet());
System.out.println("City : " + address.getCity());
System.out.println("PIN : " + address.getPin());
}
}

/////////////////////////////////////////////////////////////////////////////////

Once you deploy the above code successfully in any application server, the server will automatically creates the .wsdl file

to see the wsdl file in Jboss server after successfull deployment, type the below url in browser.

http://192.168.33.79:8080/jbossws/ and then press enter you can able to view the services running the JBoss server. If you click on the link which the service is running it will displays the complete WSDL file. Save that wsdl into any one physical location which is the base to write the Web service client program.

Writing Web Service Client

To generate the client side stub and locator classes we have to have wsdlToJava.jar, and path should be set to JDK.

Assume we have saved wsdl file in C:\ the wsdl name is Employee.wsdl

1. Open Command prompt goto C:\

2. Set the Java Path and also set the class path to wsdlToJava.jar file

3. Execute the following command from command prompt.

C:\>java org.apache.axis.wsdl.WSDL2Java Employee.wsdl

4. It will generate all the classes under the appropriate packages.

5. You need to change the URL in the Locator class.