Friday 29 May 2009

1- How to setup a JMS queue with also JTA processing

This note is about setting up a JMS queue… It is based on Understanding J2EE Transactions example of tinptB ‘s Blog: EJB in 21 Days: http://ejbvn.wordpress.com/ I appreciate her excellent English, which makes a soothing effect while trying to break through difficulties.

TINPTB’s work for the 16th Day includes all files that is necessary for this example but misses tha JMS setup part naturally, because it changes according to the application server you use. In addition to his work I added the home and interface files to his RegistrarMDB. You can find the JMS setup at the bottom.

Without any further aloboration, follows my solution below full and completely working with: Jdeveloper 11g(only the client not the deployment), WebLogic10.3, wlfullclient5, apache-ant-1.7.1 and mysql-essential-5.1.30-win32, mysql-connector-java-5.0.8 and Toad for MySQL 4.1 Freeware.

FILE: RegistrarHome.java
package jtadistrib;

import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;

public interface RegistrarHome extends EJBHome{
Registrar create() throws CreateException, RemoteException;
}

FILE: Registrar.java
package jtadistrib;

import java.rmi.RemoteException;

public interface Registrar extends javax.ejb.EJBObject{
}

FILE: RegistrarMDB.java
package jtadistrib;

import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.ejb.EJBException;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
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;
import javax.naming.NamingException;
public class RegistrarMDB implements MessageDrivenBean, MessageListener{
private MessageDrivenContext mctx =null;
public void setMessageDrivenContext(MessageDrivenContext ctx) {mctx = ctx;}
public void ejbCreate() {
System.out.println("Instance of RegistrarMDB is created...");
}
public void ejbRemove() {}

public void onMessage(Message message) {
System.out.println("RegistrarMDB.onMessage: started..");
try {
TextMessage msg = (TextMessage)message;
System.out.println("RegistrarMDB: Registrar received message: " +
msg.getText());
} catch(JMSException e) {
e.printStackTrace();
}
}
}