Friday 29 May 2009

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

FILE: Client.java
import javax.naming.*;
import javax.rmi.PortableRemoteObject;

import jtadistrib.UserManager;
import jtadistrib.UserManagerHome;

public class Client {
public static void main(String argv[]) {
// A simple client deligates the UserManager session bean
// to perform a JTA transaction. The JTA transaction will
// be started in one method, then the context will be
// propagated to other methods of the bean. Multiple resources:
// a JDBC and a JMS resource manager are invloved in the
// same distributed transaction.
System.out.print("Demonstration the use of JTA \n");
try {
InitialContext ctx = new InitialContext();
Object obj = ctx.lookup("jtadistrib/UserManagerHome");
UserManagerHome userHome = (UserManagerHome)
PortableRemoteObject.narrow(obj, UserManagerHome.class);
UserManager user = userHome.create();
// Delegate a UserManager to start a JTA transaction
System.out.println("\nStart global transaction...");
user.connectUserManager();
// Connect to a JDBC DataSource and add the enrolled course
System.out.println("Adding courses to database...");
user.addCourse(1, "CS310");
// Connect to a JMS destination and send a message
System.out.println("Notifying registrar queue...");
user.notifyRegistrar(1, "CS310");
// Commit the JTA transaction and release resources
Thread.sleep(1000);
System.out.println("Committing transaction...");
user.disconnectUserManager();
} catch(Exception e) {
System.err.println(e.toString());
}
}
}

FILE: jndi.properties
java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory
java.naming.factory.url.pkgs=weblogic.jndi.factories:weblogic.corba.j2ee.naming.url:weblogic.corba.client.naming
java.naming.provider.url=t3://localhost:7101
java.naming.security.principal=weblogic
java.naming.security.credentials=weblogic

FILE: build.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="EJB3 Tutorial" basedir="." default="deploy">

<property name="deploy.dir" value="C:/JTAdistrib/deploy" />
<property name="sourcedir" value="${basedir}/src"/>
<property name="targetdir" value="${basedir}/build"/>
<property name="librarydir" value="${basedir}/lib"/>

<path id="libraries">
<fileset dir="${librarydir}">
<include name="*.jar"/>
</fileset>
</path>

<target name="clean">
<delete dir="${targetdir}"/>
<mkdir dir="${targetdir}"/>
</target>

<target name="compile" depends="copy-resources">
<javac srcdir="${sourcedir}"
destdir="${targetdir}"
classpathref="libraries"
debug="on">
<compilerarg value="-Xlint"/>
</javac>
</target>
<target name="copy-resources">
<copy todir="${targetdir}">
<fileset dir="${sourcedir}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>

<target name="deploy" description="JARs the Task" depends="clean,copy-resources,compile">
<jar destfile="${deploy.dir}/JTAdis.jar">
<metainf dir="${sourcedir}/META-INF" />
<fileset dir="${targetdir}">
<include name="**/*.class" />
</fileset>
</jar>
</target>

<target name="undeploy" description="Undeploy jar from server">
<delete file="${deploy.dir}/JTAdis.jar" />
</target>

<target name="run" depends="compile">
<java classname="Client" classpathref="libraries">
<classpath path="${targetdir}"/>
<jvmarg value="-Djava.library.path=./lib"/>
</java>
</target>
</project>