Andre Broers’ personal blog

November 23, 2007

Create an EJB as JMS client

Filed under: ejb, glassfish, j2ee, java, jms, mdb — broersa @ 12:25 pm

In this sample we create an EJB  that calls the MDB from previous blog.

Lets start with the EJB code (don’t look at the method names, I copied from a previous example :-) )

broersa@debian1:~/work/HelloApp/HelloMDBEJB/src/com/bekijkhet$ cat HelloMDBEJBLocal.java


package com.bekijkhet;
public interface HelloMDBEJBLocal {
  public String sayHelloStateless();
  public String sayHelloStatelessLocal();
  public void setMember(String member);
}

broersa@debian1:~/work/HelloApp/HelloMDBEJB/src/com/bekijkhet$ cat HelloMDBEJB.java


package com.bekijkhet;
public interface HelloMDBEJB {
  public String sayHelloStateless();
  public String sayHelloStatelessRemote();
  public void setMember(String member);
}
 

broersa@debian1:~/work/HelloApp/HelloMDBEJB/src/com/bekijkhet$ cat HelloMDBEJBBean.java


package com.bekijkhet;
import javax.ejb.Stateless;
import javax.ejb.Remote;
import javax.ejb.Local;
import javax.jms.*;
import javax.naming.*;
@Stateless
@Remote(HelloMDBEJB.class)
@Local(HelloMDBEJBLocal.class)
public class HelloMDBEJBBean implements HelloMDBEJB,HelloMDBEJBLocal {

  private String member = "Not set!";

  public String sayHelloStateless() {
          try {
            Context ctx = new InitialContext();
            ConnectionFactory     connectionFactory = (ConnectionFactory)ctx.lookup("jms/ConnectionFactory");
            Queue     queue = (Queue)ctx.lookup("jms/SampleQueue");
            javax.jms.Connection  connection = connectionFactory.createConnection();
            javax.jms.Session        session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
            MessageProducer messageProducer = session.createProducer(queue);
            TextMessage message = session.createTextMessage();
            message.setText("my test message");
            System.out.println( "MDBEJB:"+ message.getText());
            messageProducer.send(message);
            connection.close();
          } catch (Exception e) {System.out.println(e.toString());}
    return "Hello Stateless "+member;
  }
  public String sayHelloStatelessLocal() {
    return "Hello Local Stateless "+member;
  }
  public String sayHelloStatelessRemote() {
    return "Hello Remote Stateless "+member;
  }
  public void setMember(String member) {
    this.member=member;
  }
}

broersa@debian1:~/work/HelloApp/HelloMDBEJB$ cat build.xml


<project name="HelloEJB" default="dist" basedir=".">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}"/>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/HelloMDBEJB.jar" basedir="${build}"/>
  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>

after this build with

asant dist

and deploy

asadmin deploy dist/HelloMDBEJB.jar

than the client to call the EJB which is similar to the client  in the previous samples:

broersa@debian1:~/work/HelloApp/HelloClient/src/com/bekijkhet/helloclient$ cat HelloMDBEJBClient.java


package com.bekijkhet.helloclient;
import javax.naming.*;
import com.bekijkhet.HelloMDBEJB;
public class HelloMDBEJBClient {
  public static void main(String[] args) {
    try {
      InitialContext ctx = new InitialContext();
      HelloMDBEJB h = (HelloMDBEJB)ctx.lookup("com.bekijkhet.HelloMDBEJB");

      System.out.println(h.sayHelloStateless());
    }
    catch (Exception e) {
      System.out.println(e);
      e.printStackTrace();
    }
  }
}

compile:

javac -cp $GLASSFISH_HOME/lib/appserv-rt.jar:$GLASSFISH_HOME/lib/javaee.jar:$HOME/work/HelloApp/HelloMDBEJB/dist/HelloMDBEJB.jar:. -d . HelloMDBEJBClient.java

run:

java -cp $GLASSFISH_HOME/lib/appserv-rt.jar:$GLASSFISH_HOME/lib/javaee.jar:$HOME/work/HelloApp/HelloMDBEJB/dist/HelloMDBEJB.jar:. com.bekijkhet.helloclient.HelloMDBEJBClient
Hello Stateless Not set!

and the output in the $GLASSFISH_HOME/domains/domain1/logs/server.log

[#|2007-11-23T13:49:49.076+0100|INFO|sun-appserver9.1|javax.enterprise.system.stream.out|_ThreadID=26;_ThreadName=p: thread-pool-1; w: 61;|
MDBEJB:my test message|#]

[#|2007-11-23T13:49:49.091+0100|INFO|sun-appserver9.1|javax.enterprise.system.stream.out|_ThreadID=26;_ThreadName=p: thread-pool-1; w: 61;|
Got message: my test message|#]

Create a Message Driven Bean (MDB) and stand alone client

Filed under: ejb, glassfish, j2ee, java, jms, mdb — broersa @ 10:58 am

Next example is a message driven bean deployed on glassfish that gets called by a stand alone client. Let start with creating the JMS Connection Factory and the JMS Queue on glassfish.

Issue the following statement to create the connection factory:

asadmin create-jms-resource –restype javax.jms.ConnectionFactory jms/ConnectionFactory

After this create the resource destination:

asadmin create-jmsdest -T queue sampleQueue

Create the JMS destination:

asadmin create-jms-resource –restype javax.jms.Queue –property Name=sampleQueue jms/SampleQueue

Now we can create the Message Driven Bean:

broersa@debian1:~/work/HelloApp/HelloMDB/src/com/bekijkhet$ cat HelloMDB.java


package com.bekijkhet;
import javax.ejb.MessageDriven;
import javax.jms.MessageListener;
import javax.jms.Message;
import javax.jms.TextMessage;

@MessageDriven(mappedName="jms/SampleQueue")
public class HelloMDB implements MessageListener {

    public void onMessage(Message msg) {
      try {
        if (msg instanceof TextMessage) {
          TextMessage txtmsg = (TextMessage) msg;
          System.out.println("Got message: "+txtmsg.getText());
        }
      } catch (Exception e) { System.out.println(e.toString()); }
    }

}

broersa@debian1:~/work/HelloApp/HelloMDB$ cat build.xml


<project name="HelloMDB" default="dist" basedir=".">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}"/>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/HelloMDB.jar" basedir="${build}"/>
  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>

Build and deploy the MDB:asant dist

asadmin deploy dist/HelloMDB.jar

Let’s go on creating the stand alone client to call the MDB.

broersa@debian1:~/work/HelloApp/HelloClient/src/com/bekijkhet/helloclient$ cat HelloMDBClient.java


package com.bekijkhet.helloclient;
import javax.naming.*;
import javax.jms.*;

public class HelloMDBClient {
  public static void main(String args[]) {
    try {
      // will get the local default context from appserv-rt.jar
      Context ctx = new InitialContext();
      ConnectionFactory connectionFactory = (ConnectionFactory)ctx.lookup("jms/ConnectionFactory");
      Queue queue = (Queue)ctx.lookup("jms/SampleQueue");
      javax.jms.Connection connection = connectionFactory.createConnection();
      javax.jms.Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
      MessageProducer messageProducer = session.createProducer(queue);
      TextMessage message = session.createTextMessage();
      message.setText("my own text message");
      System.out.println( "Send: "+ message.getText());
      messageProducer.send(message);
      connection.close();
    } catch (Exception e) {System.out.println(e.toString());}
  }
}

Compile and run:

javac -cp $GLASSFISH_HOME/lib/appserv-rt.jar:$GLASSFISH_HOME/lib/javaee.jar:. -d . HelloMDBClient.java

java -cp $GLASSFISH_HOME/lib/appserv-rt.jar:$GLASSFISH_HOME/lib/javaee.jar:$GLASSFISH_HOME/lib/install/applications/jmsra/imqjmsra.jar:$GLASSFISH_HOME/lib/appserv-admin.jar:. com.bekijkhet.helloclient.HelloMDBClient

<snipped a lot of initialisation output..>

Send: my own text message

When we look at the server log file ($GLASSFISH_HOME/domains/domain1/logs/server.log) we can see the MDB got fired:

[#|2007-11-23T12:17:45.309+0100|INFO|sun-appserver9.1|javax.enterprise.system.stream.out|_ThreadID=23;_ThreadName=p: thread-pool-1; w: 34;|
Got message: my own text message|#]
After this my client app (HelloMDBClient) hangs….. Press ctrl-c to end the process. I can’t find a solution to get the program exit normally… It looks like another thread got starten to listen for incomming messages??

In the next sample I create an EJB to send messages which will be run from inside the container. This seams to work without hanging. Maybe someone can explain?

November 21, 2007

Stateful and Stateless EJB sample

Filed under: ejb, glassfish, j2ee, java — broersa @ 6:58 pm

In this example I show the difference between Stateful en Stateless EJB’s. First I create and deploy the stateful bean.

broersa@debian1:~/work/HelloApp/HelloStateful/src/com/bekijkhet$ cat HelloStateful.java


package com.bekijkhet;
public interface HelloStateful {
  public String sayHelloStateful();
  public String sayHelloStatefulRemote();
  public void setMember(String member);
}

broersa@debian1:~/work/HelloApp/HelloStateful/src/com/bekijkhet$ cat HelloStatefulLocal.java


package com.bekijkhet;
public interface HelloStatefulLocal {
  public String sayHelloStateful();
  public String sayHelloStatefulLocal();
  public void setMember(String member);
}

broersa@debian1:~/work/HelloApp/HelloStateful/src/com/bekijkhet$ cat HelloStatefulBean.java


package com.bekijkhet;
import javax.ejb.Stateful;
import javax.ejb.Remote;
import javax.ejb.Local;
@Stateful
@Remote(HelloStateful.class)
@Local(HelloStatefulLocal.class)
public class HelloStatefulBean implements HelloStateful,HelloStatefulLocal {

  private String member = "Not set!";

  public String sayHelloStateful() {
    return "Hello Stateful "+member;
  }
  public String sayHelloStatefulLocal() {
    return "Hello Local Stateful "+member;
  }
  public String sayHelloStatefulRemote() {
    return "Hello Remote Stateful "+member;
  }
  public void setMember(String member) {
    this.member=member;
  }
}

broersa@debian1:~/work/HelloApp/HelloStateful$ cat build.xml


<project name="HelloEJB" default="dist" basedir=".">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}"/>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/HelloStateful.jar" basedir="${build}"/>
  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>

Then build: asant distThen deploy: asadmin deploy dist/HelloStateful.jarNow the stateless bean:

broersa@debian1:~/work/HelloApp/HelloStateless/src/com/bekijkhet$ cat HelloStateless.java


package com.bekijkhet;
public interface HelloStateless {
  public String sayHelloStateless();
  public String sayHelloStatelessRemote();
  public void setMember(String member);
}

broersa@debian1:~/work/HelloApp/HelloStateless/src/com/bekijkhet$ cat HelloStatelessLocal.java


package com.bekijkhet;
public interface HelloStatelessLocal {
  public String sayHelloStateless();
  public String sayHelloStatelessLocal();
  public void setMember(String member);
}

broersa@debian1:~/work/HelloApp/HelloStateless/src/com/bekijkhet$ cat HelloStatelessBean.java


package com.bekijkhet;
import javax.ejb.Stateless;
import javax.ejb.Remote;
import javax.ejb.Local;
@Stateless
@Remote(HelloStateless.class)
@Local(HelloStatelessLocal.class)
public class HelloStatelessBean implements HelloStateless,HelloStatelessLocal {

  private String member = "Not set!";

  public String sayHelloStateless() {
    return "Hello Stateless "+member;
  }
  public String sayHelloStatelessLocal() {
    return "Hello Local Stateless "+member;
  }
  public String sayHelloStatelessRemote() {
    return "Hello Remote Stateless "+member;
  }
  public void setMember(String member) {
    this.member=member;
  }
}

broersa@debian1:~/work/HelloApp/HelloStateless$ cat build.xml


<project name="HelloEJB" default="dist" basedir=".">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}"/>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/HelloStateless.jar" basedir="${build}"/>
  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>

And finally the client:broersa@debian1:~/work/HelloApp/HelloClient/src/com/bekijkhet/helloclient$ cat HelloStatefulClient.java


package com.bekijkhet.helloclient;
import javax.naming.*;
import com.bekijkhet.HelloStateful;
import com.bekijkhet.HelloStateless;
public class HelloStatefulClient {
  public static void main(String[] args) {
    try {
      InitialContext ctx = new InitialContext();
      HelloStateful h = (HelloStateful)ctx.lookup("com.bekijkhet.HelloStateful");
      HelloStateful i = (HelloStateful)ctx.lookup("com.bekijkhet.HelloStateful");
      System.out.println(h.sayHelloStateful());
      h.setMember("Initialized!");
      System.out.println(h.sayHelloStateful());
      System.out.println(i.sayHelloStateful());
    }
    catch (Exception e) {
      System.out.println(e);
      e.printStackTrace();
    }
    try {
      InitialContext ctx = new InitialContext();
      HelloStateless j = (HelloStateless)ctx.lookup("com.bekijkhet.HelloStateless");
      HelloStateless k = (HelloStateless)ctx.lookup("com.bekijkhet.HelloStateless");
      System.out.println(j.sayHelloStateless());
      j.setMember("Init!");
      System.out.println(j.sayHelloStateless());
      System.out.println(k.sayHelloStateless());
    }
    catch (Exception e) {
      System.out.println(e);
      e.printStackTrace();
    }
  }
}

broersa@debian1:~/work/HelloApp/HelloClient/src/com/bekijkhet/helloclient$ javac -cp $GLASSFISH_HOME/lib/appserv-rt.jar:$GLASSFISH_HOME/lib/javaee.jar:$HOME/work/HelloApp/HelloStateful/dist/HelloStateful.jar:$HOME/work/HelloApp/HelloStateless/dist/HelloStateless.jar:. -d . HelloStatefulClient.javaRun for the first time:

broersa@debian1:~/work/HelloApp/HelloClient/src/com/bekijkhet/helloclient$ java -cp $GLASSFISH_HOME/lib/appserv-rt.jar:$GLASSFISH_HOME/lib/javaee.jar:$HOME/work/HelloApp/HelloStateful/dist/HelloStateful.jar:$HOME/work/HelloApp/HelloStateless/dist/HelloStateless.jar:. com.bekijkhet.helloclient.HelloStatefulClient
Hello Stateful Not set!
Hello Stateful Initialized!
Hello Stateful Not set!
Hello Stateless Not set!
Hello Stateless Init!
Hello Stateless Init!
Run for the second time:

broersa@debian1:~/work/HelloApp/HelloClient/src/com/bekijkhet/helloclient$ java -cp $GLASSFISH_HOME/lib/appserv-rt.jar:$GLASSFISH_HOME/lib/javaee.jar:$HOME/work/HelloApp/HelloStateful/dist/HelloStateful.jar:$HOME/work/HelloApp/HelloStateless/dist/HelloStateless.jar:. com.bekijkhet.helloclient.HelloStatefulClient
Hello Stateful Not set!
Hello Stateful Initialized!
Hello Stateful Not set!
Hello Stateless Init!
Hello Stateless Init!
Hello Stateless Init!

Notice that everytime you create a new stateful session bean you get a new fresh instance. But when you set it it keeps its state during the client session.

When a stateless session bean is created you always get the same instance (from out of a pool). It looks like the state is kept, but this is server independant and can never been garenteed. When we call the client again after +/- half an hour we get a fresh stateless bean, so it gets cleaned up. This clean up process can happen all the time, even between two calls of the same instance in the same client session.

November 20, 2007

Client calls EJB on Glassfish which calls EJB on OC4J 11G

Filed under: ejb, glassfish, j2ee, java, oc4j, oracle — broersa @ 9:21 pm

In this blog I’m showing a way to call an EJB in an OC4J container from an EJB in a Glassfish container.

Let’s start with the EJB in the OC4J 11G container:

broersa@debian1:~/work/HelloApp/HelloOrion/src/com/bekijkhet/orion$ cat HelloOrionLocal.java


package com.bekijkhet.orion;
public interface HelloOrionLocal {
  public String sayHello();
  public String sayHelloLocal();
}

broersa@debian1:~/work/HelloApp/HelloOrion/src/com/bekijkhet/orion$ cat HelloOrion.java


package com.bekijkhet.orion;
public interface HelloOrion {
  public String sayHello();
  public String sayHelloRemote();
}

broersa@debian1:~/work/HelloApp/HelloOrion/src/com/bekijkhet/orion$ cat HelloOrionBean.java


package com.bekijkhet.orion;
import javax.ejb.Stateless;
import javax.ejb.Remote;
import javax.ejb.Local;
@Stateless
@Remote(HelloOrion.class)
@Local(HelloOrionLocal.class)
public class HelloOrionBean implements HelloOrion,HelloOrionLocal {
  public String sayHello() {
    return "Hello Orion!!!!";
  }
  public String sayHelloLocal() {
    return "Hello Local Orion!!!!";
  }
  public String sayHelloRemote() {
    return "Hello Remote Orion!!!!";
  }
}

broersa@debian1:~/work/HelloApp/HelloOrion$ cat build.xml


<project name="HelloEJB" default="dist" basedir=".">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}"/>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/HelloOrion.jar" basedir="${build}"/>
  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>

Type ‘asant dist’ to create the EJB.Deploy the Bean to the OC4J container:

java -jar $ORACLE_HOME/j2ee/home/admin_client.jar deployer:oc4j:localhost oc4jadmin welcome -deploy -file $HOME/work/HelloApp/HelloOrion/dist/HelloOrion.jar -deploymentName HelloOrion

Now it is time to create the Bean in the Glassfish container which calls the HelloOrion Bean.First we need the Interface from the HelloOrion Bean that we gonna call in the OC4J container. I could include it in a jar file, but for the simplicity I just copy it in the HelloEJB project.

broersa@debian1:~/work/HelloApp/HelloEJB/src/com/bekijkhet/orion$ cat HelloOrion.java


package com.bekijkhet.orion;
public interface HelloOrion {
  public String sayHello();
  public String sayHelloRemote();
}

Then we need the actual Bean.

broersa@debian1:~/work/HelloApp/HelloEJB/src/com/bekijkhet$ cat Hello.java


package com.bekijkhet;
public interface Hello {
  public String sayHello();
}

broersa@debian1:~/work/HelloApp/HelloEJB/src/com/bekijkhet$ cat HelloBean.java


package com.bekijkhet;
import javax.ejb.Stateless;
import javax.ejb.EJB;
import javax.ejb.Remote;
import java.util.Properties;
import java.net.URL;
import javax.naming.*;
import com.bekijkhet.orion.HelloOrion;
@Stateless
@Remote(Hello.class)
public class HelloBean implements Hello {

  static String CONFIG_PROPERTIES = "ejb.properties";

  public String sayHello() {
    URL url = null;
    Properties props = null;
    HelloOrion h = null;
    // Use the ClassLoader to get the properties of the Bean to call
    url = Thread.currentThread().getContextClassLoader().getResource(CONFIG_PROPERTIES);
    if (url == null) {
      return "The configuration could not be found: " + CONFIG_PROPERTIES;
    } else {
      props = new Properties();
      try {
        props.load(url.openStream());
      }
      catch (java.io.IOException e) {
        return "Could not read configuration file from URL";
      }
    }
    try {
      Context ctx = new InitialContext(props);
      h = (HelloOrion)ctx.lookup("HelloOrionBean");
    } catch ( Exception e) { return e.toString(); }
    return "Hello From Glassfish!!!! "  + h.sayHelloRemote();
  }
}

broersa@debian1:~/work/HelloApp/HelloEJB/properties$ cat ejb.properties


java.naming.factory.initial=com.evermind.server.rmi.RMIInitialContextFactory
java.naming.provider.url=ormi://localhost/HelloOrion
java.naming.security.principal=oc4jadmin
java.naming.security.credentials=welcome

broersa@debian1:~/work/HelloApp/HelloEJB$ cat build.xml

<project name="HelloEJB" default="dist" basedir=".">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}"/>
    <copy todir="${build}">
                        <fileset dir="properties">
                                <include name="*" />
                        </fileset>
                   </copy>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/HelloBean.jar" basedir="${build}"/>
  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>

Type asant dist to create the distribution jar.Type asadmin deploy dist/HelloBean.jar to deploy the Bean to glassfish.In this bean I use the classloader to get the properties to call the OC4J bean from a flat file which must be in the Classpath. After this I create a Inital context. To create this context Glassfish needs the $ORACLE_HOME/j2ee/home/oc4jclient.jar in $GLASSFISH/lib directory. After copying this jar you need to restart the Glassfish server.Now it is time to create a simple client to call the EJB in the glassfish container which calls the EJB in OC4J.

broersa@debian1:~/work/HelloApp/HelloClient/src/com/bekijkhet/helloclient$ cat HelloClient.java


package com.bekijkhet.helloclient;
import javax.naming.*;
import com.bekijkhet.Hello;
public class HelloClient {
  public static void main(String[] args) {
    try {
      InitialContext ctx = new InitialContext();
      Hello h = (Hello)ctx.lookup("com.bekijkhet.Hello");
     System.out.println(h.sayHello());
    }
    catch (Exception e) {
      System.out.println(e);
      e.printStackTrace();
    }
  }
}

javac -cp $GLASSFISH_HOME/lib/appserv-rt.jar:$GLASSFISH_HOME/lib/javaee.jar:$HOME/work/HelloApp/HelloEJB/dist/HelloBean.jar:. -d . HelloClient.java

java -cp $GLASSFISH_HOME/lib/appserv-rt.jar:$GLASSFISH_HOME/lib/javaee.jar:$HOME/work/HelloApp/HelloEJB/dist/HelloBean.jar:. com.bekijkhet.helloclient.HelloClient

Hello From Glassfish!!!! Hello Remote Orion!!!!

November 9, 2007

oc4j 11g Spy tool

Filed under: j2ee, oc4j, oracle — broersa @ 11:30 am

To get some extra (clickable) info about the oc4j appserver try the following link:

http://localhost:8888/dms0/Spy

Stateless EJB and client using OC4J 11G

Filed under: ejb, j2ee, java, oc4j, oracle — broersa @ 10:50 am

In a previous blog I showed a sample of a helloworld stateless ejb on glassfish. In this entry I use the same EJB but I will deploy the bean to the OC4J 11G container. I will use the same client to connect to the OC4J deployed bean. The principles are the same but to connect to the OC4J container we’ll need some other libraries and some new config files. Lets start with the deployment of the EJB to the OC4J container:

java -jar $ORACLE_HOME/j2ee/home/admin_client.jar deployer:oc4j:localhost oc4jadmin welcome -deploy -file $HOME/work/HelloApp/HelloEJB/dist/lib/HelloBean-<date>.jar -deploymentName HelloEJB

After deploying we get to the client side. First we need the client source.

broersa@debian1:~/work/HelloApp/HelloClient/src/com/bekijkhet/helloclient$ cat HelloClient2.java


package com.bekijkhet.helloclient;
import javax.naming.*;
import com.bekijkhet.Hello;
public class HelloClient2 {
  public static void main(String[] args) {
    try {
      InitialContext ctx = new InitialContext();
      // Piece of code to list all remote JNDI resources
      //NamingEnumeration e = ctx.list("");
      //while (e.hasMore()) {
      //  System.out.println(e.next());
      //}
      Hello h = (Hello)ctx.lookup("HelloBean");
      System.out.println(h.sayHello());
      System.out.println(h.sayHelloRemote());
      // Will fail because we call the Remote Business interface
      // System.out.println(h.sayHelloLocal());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Notice that by default the lookup needs the Bean name HelloBean.

For OC4J to lookup the InitialContext we need a properties file. This file has to be in the classpath. We use the current directory.

broersa@debian1:~/work/HelloApp/HelloClient/src/com/bekijkhet/helloclient$ cat jndi.properties

java.naming.factory.initial=com.evermind.server.ApplicationClientInitialContextFactory
java.naming.provider.url=ormi://localhost/HelloEJB
java.naming.security.principal=oc4jadmin
java.naming.security.credentials=welcome

OC4J also expects a application-client.xml deployment descriptior:

broersa@debian1:~/work/HelloApp/HelloClient/src/com/bekijkhet/helloclient/META-INF$ cat application-client.xml


<?xml version="1.0" encoding="UTF-8"?>

<application-client xmlns="http://java.sun.com/xml/ns/j2ee"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
                    http://java.sun.com/xml/ns/j2ee/application-client_1_4.xsd"
                    version="1.4">

  <display-name>Client of the earsample</display-name>
  <description>client of the earsample</description>

</application-client>

After this we can compile the client:

javac -cp $ORACLE_HOME/j2ee/home/oc4j.jar:$ORACLE_HOME/j2ee/home/oc4jclient.jar:$HOME/work/HelloApp/HelloEJB/dist/lib/HelloBean-<date>.jar:. -d . HelloClient2.java

And then run the client:

java -cp $ORACLE_HOME/j2ee/home/oc4j.jar:$ORACLE_HOME/j2ee/home/oc4jclient.jar:$HOME/work/HelloApp/HelloEJB/dist/lib/HelloBean-<date>.jar:. com.bekijkhet.helloclient.HelloClient2

Hello World!!!!
Hello Remote World!!!!

Install OC4J 11G stand alone on Linux

Filed under: j2ee, java, oc4j, oracle — broersa @ 10:11 am

The way I did it was:

  • Install a jdk 1.5 or higher and set env var: export JAVA_HOME=$HOME/<jdkdir>
  • Download the zip file from http://www.oracle.com/technology/tech/java/oc4j/11/index.html
  • Create a directory oc4j in your homedir from that directory unzip the oracle zip file
  • Create a env var: export ORACLE_HOME=$HOME/oc4j
  • cd $ORACLE_HOME/bin
  • ./oc4j -start
  • enter an admin password (since this is the first start)
  • check the serverstatus on http://localhost:8888

Have fun..

Stateless EJB and client using Glassfish

Filed under: ejb, glassfish, j2ee, java — broersa @ 7:36 am

Remote interface to the Bean:
broersa@debian1:~/work/HelloApp/HelloEJB/src/com/bekijkhet$ cat Hello.java

 package com.bekijkhet;
 public interface Hello {
   public String sayHello();
   public String sayHelloRemote();
 }

Local interface to the Bean:
broersa@debian1:~/work/HelloApp/HelloEJB/src/com/bekijkhet$ cat HelloLocal.java

 package com.bekijkhet;
 public interface HelloLocal {
   public String sayHello();
   public String sayHelloLocal();
 }

Bean himself :
broersa@debian1:~/work/HelloApp/HelloEJB/src/com/bekijkhet$ cat HelloBean.java

 package com.bekijkhet;
 import javax.ejb.Stateless;
 import javax.ejb.Remote;
 import javax.ejb.Local;
 @Stateless
 @Remote(Hello.class)
 @Local(HelloLocal.class)
 public class HelloBean implements Hello,HelloLocal {
   public String sayHello() {
     return "Hello World!!!!";
   }
   public String sayHelloLocal() {
     return "Hello Local World!!!!";
   }
   public String sayHelloRemote() {
     return "Hello Remote World!!!!";
   }
 }

Build.xml script:
broersa@debian1:~/work/HelloApp/HelloEJB$ cat build.xml

<project name="HelloEJB" default="dist" basedir=".">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/lib/HelloBean-${DSTAMP}.jar" basedir="${build}"/>
  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>

Do a asant dist to create the EJB jar file and use asadmin deploy <jarfile> to deploy the EJB to Glassfish.
As an example I wil create a simple stand alone client that will call the Remote interface of the EJB. Local isn’t possible because I create a stand alone client which does not reside inside the container.Client code:
broersa@debian1:~/work/HelloApp/HelloClient/src/com/bekijkhet/helloclient$ cat HelloClient.java

package com.bekijkhet.helloclient;
import javax.naming.*;
import com.bekijkhet.Hello;

public class HelloClient {
  public static void main(String[] args) {
    try {
      InitialContext ctx = new InitialContext();
      // Piece of code to list all remote JNDI resources
      //NamingEnumeration e = ctx.list("");
      //while (e.hasMore()) {
      //  System.out.println(e.next());
      //}

      Hello h = (Hello)ctx.lookup("com.bekijkhet.Hello");
      System.out.println(h.sayHello());
      System.out.println(h.sayHelloRemote());
      // Will fail because we call the Remote Business interface
      // System.out.println(h.sayHelloLocal());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Use the next steps to compile and run the code:broersa@debian1:~/work/HelloApp/HelloClient/src/com/bekijkhet/helloclient$ javac -cp $GLASSFISH_HOME/lib/javaee.jar:$HOME/work/HelloApp/HelloEJB/dist/lib/HelloBean-<date>.jar:. -d . HelloClient.java

broersa@debian1:~/work/HelloApp/HelloClient/src/com/bekijkhet/helloclient$ java -cp $GLASSFISH_HOME/lib/javaee.jar:$GLASSFISH_HOME/lib/appserv-rt.jar:$HOME/work/HelloApp/HelloEJB/dist/lib/HelloBean-20071108.jar:. com.bekijkhet.helloclient.HelloClient

Hello World!!!!
Hello Remote World!!!!

This is a simple Hello world EJB sample. Have fun.

November 4, 2007

Oracle SOA Suite won’t install on top of Oracle DB 11g

Filed under: oracle, soa suite — broersa @ 11:35 am

Just came to the conclusion that soa suite won’t install on top of an Oracle 11G Database. The installation of the IRCA schema’s went fine, but during the real installation process it claims it doesn’t find a suiteable database.

Hmm, have to install another vmware instance with Oracle 10G Database.

November 3, 2007

Install soa suite on Oracle Enterprise Linux

Filed under: oracle, soa suite — broersa @ 8:00 pm

While installing soa suite on oracle enterprise linux the /etc/redhat-release file must be changed. Use the following string in the file:

mv /etc/redhat-release /etc/redhat-release.bak

echo “Red Hat Enterprise Linux AS release 4 (Nahant Update 1)” > /etc/redhat-release

Blog at WordPress.com.