Archive for the ‘Java EE’ Category

MDB deployment in OC4J

December 24, 2006

1. Create the MDB(normal java class with @MessageDriven annotation). Set the ActivationConfigs.

@MessageDriven(
activationConfig = {
@ActivationConfigProperty(propertyName=”connectionFactoryJndiName”, propertyValue=”jms/TopicConnectionFactory”),
@ActivationConfigProperty(propertyName=”destinationName”,
propertyValue=”jms/demoTopic”),
@ActivationConfigProperty(propertyName=”destinationType”, propertyValue=”javax.jms.Topic”),
@ActivationConfigProperty(propertyName=”messageSelector”, propertyValue=”RECIPIENT = ‘MDB’”) } )

2. Create EJB jar deployment profile(MDBServer.deploy) for the above MDB and deploy it to OC4J server(application name = MDBServer)

3. In same project, create the MDBClient. Inject the resources @Resource(name=”jms/demoQueue”) and @Resource(name=”jms/TopicConnectionFactory”). Use connectionFactory and create connection, then session, message and send the message using producer.

4. In Jdeveloper create jndi.properties file with:

java.naming.factory.initial= oracle.j2ee.naming.ApplicationClientInitialContextFactory
java.naming.provider.url=ormi://localhost:23791/MDBServer
java.naming.security.principal=oc4jadmin
java.naming.security.credentials=welcome

5. Create client Jar deployment profile from Jdeveloper. This creates application-client.xml. Modify application-client.xml and add following lines for the resource reference entries(Make sure to mention the main class file name in descriptor creation)

<application-client 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″ xmlns=”http://java.sun.com/xml/ns/j2ee“>
<display-name>MDB1-app-client</display-name>
<resource-env-ref>
<resource-env-ref-name>jms/demoTopic</resource-env-ref-name>
<resource-env-ref-type>javax.jms.Topic</resource-env-ref-type>
</resource-env-ref>
<resource-env-ref>
<resource-env-ref-name>jms/TopicConnectionFactory </resource-env-ref-name>
<resource-env-ref-type>javax.jms.TopicConnectionFactory </resource-env-ref-type>
</resource-env-ref>
</application-client>

6. Deploy the deployment profile MDBClient.deploy to MDBClient.jar.

7. From command prompt containing the jar file, set class path and run :

D:\installs\JdevStudio10131\jdev\mywork\MDB\MDB1\deploy>set CLASSPATH =.\;D:\installs\oc4j\j2ee\home\oc4jclient.jar;D:\installs\oc4j\j2ee\home\lib\javax77.jar;

D:\installs\JdevStudio10131\jdev\mywork\MDB\MDB1\deploy>java oracle.oc4j.appclient.AppClientContainer MDBClient.jar

Output from the application server log will contain:

06/12/24 20:26:44 onMessage() – Message[ID:Oc4jJMS.Message.krishnamoorthy.64e0c3
ab:10fb4f6b670:-8000.4]

onMessage method of the MDB just does a system.out.println(message)

Note: Without creating jar of client files and if I directly run using:

D:\installs\JdevStudio10131\jdev\mywork\MDB\MDB1\classes>java  -Djava.naming.factory.initial=oracle.j2ee.naming.ApplicationClientInitialContextFactory -Djava.naming.provider.url=ormi://localhost:23791/MDBServer -Djava.naming.security.principal=oc4jadmin -Djava.naming.security.credentials=welcome myJMSClient.TestJMSClient

I get

Exception in thread “main” java.lang.NullPointerException
at myJMSClient.TestJMSClient.main(TestJMSClient.java:22)

So, should a JMS client be run always using AppClientContainer as :

java oracle.oc4j.appclient.AppClientContainer MDBClient.jar

Running EJB Client Standalone

December 22, 2006

HelloWorld.java

package myEJB;

import javax.ejb.Remote;

@Remote
public interface HelloWorld {
public String sayHello(String name);
}

HelloWorldBean.java

package myEJB;

import javax.ejb.Stateless;

@Stateless
public class HelloWorldBean implements HelloWorld {
private int count;

public String sayHello(String name) {
count++;
System.out.println(“sayHello method from Bean “);
return “Hello ” + name + ” Count  : ” + count;
}
}

Compile the classes and run in embedded oc4j of jdeveloper. Or create a ejb jar deployment profile and deploy it to standalone oc4j server. Deployment name is the application name in the standalone oc4j. Let us say application is HelloWorldDeploy. Ear file contains jar file and META-INF. Inside META-INF is the application.xml containing the name of ejb module jar file. Jar file contains the interface and bean classes.  After deploying, create a test client as follows:

HelloWorldClient.java

package myEJB;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class HelloWorldClient {
public static void main(String [] args) {
try {
final Context context = new InitialContext();
HelloWorld helloWorld = (HelloWorld)context.lookup(“java:comp/env/ejb/HelloWorldBean”);
// Call any of the Remote methods below to access the EJB
System.out.println(helloWorld.sayHello( “Krishna” ));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

To invoke ejb method from command line:

D:\work\MyWork\ejb\ejb3>set CLASSPATH=D:\installs\JdevStudio10131\j2ee\home\oc4jclient.jar;.\;

D:\work\MyWork\ejb\ejb3>java -Djava.naming.factory.initial=oracle.j2ee.naming.Ap
plicationClientInitialContextFactory -Djava.naming.provider.url=ormi://localhost
:23791/HelloWorldDeploy -Djava.naming.security.principal=oc4jadmin -Djava.naming
.security.credentials=welcome myEJB.HelloWorldClient

Output is : Hello Krishna Count  : 1

Transaction Isolation Level

December 21, 2006

Good link for getting a refresh on the isolation level: http://www.oracle.com/technology/oramag/oracle/05-nov/o65asktom.html

Isolation Level Dirty Read Nonrepeatable Read Phantom Read
READ UNCOMMITTED Permitted Permitted Permitted
READ COMMITTED Permitted Permitted
REPEATABLE READ Permitted
SERIALIZABLE

Oracle supports READ COMMITTED and SERIALIZABLE. Oracle doesn’t allow dirty reads(READ UNCOMMITTED). It allows a third isolation level which is READ ONLY. No insert, update, delete can be done in this mode.

RMI tutorial on SUN

December 20, 2006

1. Start the rmiregistry at the default port 1099. (rmiregistry.exe)

2. Start the Local Http server for delivering class files between server-RMI and RMI-Client.

D:\work\MyWork\rmi\suntutorial>java examples.classServer.ClassFileServer 1185  D:\work\MyWork\rmi\suntutorial

3. Then start the ComputeEngine server using :

D:\work\MyWork\rmi\suntutorial>java -cp .\;compute.jar -Djava.rmi.server.codebase=http://pc-krsethur-in:1185/ -Djava.rmi.server.hostname=pc-krsethur-in -Djava.security.policy=D:\work\MyWork\rmi\suntutorial\server.policy engine.ComputeEngine

4. Remove the client files from the suntutorial folder and place it in a folder named test under this suntutorial so that RMI will get the files using http from the client(thus, the above classpath doesn’t include the client classes).

5. server.policy should have the line

grant  {
    permission java.net.SocketPermission “*:*”, “listen,accept,connect”;
};

for the binding of remote object to the registry(1099).

6. Now run the client as

D:\work\MyWork\rmi\suntutorial\test>java -cp .\;..\compute.jar; -Djava.rmi.server.codebase=http://pc-krsethur-in:1185/test/ -Djava.security.policy=D:\work\MyWork\rmi\suntutorial\client.policy client.ComputePi pc-krsethur-in 45

Output
3.141592653589793238462643383279502884197169399

7.  Client.policy should have similar line above(5)

8. here -Djava.rmi.server.codebase=http://pc-krsethur-in:1185/test/ will be set because during client execution, we call executeTask on the proxy remote object. The remote object running in the RMI runtime will delegate to the task object. Now, the task object is still not present in the RMI runtime and will give class not found exception. To prevent that we set the codebase to http://pc-krsethur-in:1185/test/. Here pc-krsethur-in:1185 will deliver files present under D:\work\MyWork\rmi\suntutorial. However, the task class is present in D:\work\MyWork\rmi\suntutorial\test folder. Hence, we append the test/ in the URL for the codebase. Our local file server will serve the classes from D:\work\MyWork\rmi\suntutorial\test for RMI runtime during client execution. Similarly, when the server was started, during binding of remote object, the interface classes(Compute.class and Task.class) for the RMI runtime was served by -Djava.rmi.server.codebase=http://pc-krsethur-in:1185/.

9. After a single execution of both server and client, the classes(Compute.class and Task.class) for the server execution and the class(Task.class) from client execution will be present in the RMI runtime and hence if the local file server is stopped at this time and the server is aborted and restarted, it will work fine without any error about class not found. Similarly if the client is run now also, the client will work fine, because the Task.class is already loaded in the RMI runtime.

10. Now if we stop the rmiregistry.exe(thus all the classes loaded in RMI runtime are gone) and restart the rmiregistry and then start the server or client(with the local file server stopped), they will fail with class not found exception.

Typical JCA 1.5 architecture application

September 5, 2006

arch.gif

OC4J – Connector Sample configuration

September 5, 2006

This is an example of how a oracle JMS provider is accessed by application server through the JCA architecture.

First the JMS provider is configured in application.xml through the following lines:

<resource-provider class=”com.evermind.server.jms.Oc4jResourceProvider” name=”oc4jjms”>
<description>oc4j-jms loop back resource provider</description>
<property name=”dummy” value=”dummy”/>
</resource-provider>

This is a loop back resource provider. Setting some additional properties like addressList pointing to JMS provider service URL in other machine will cause the message, connection etc., to be created in other machine.

This resource provider is accessed using JCA by means of resource adapters. In J2EE_home/config directory: oc4j-connectors.xml will have:

<connector name=”OracleASjms” path=”OracleASjms.rar”>
<config-property name=”lookupMethod” value=”resourceProvider”/>
<config-property name=”resourceProviderName” value=”oc4jjms”/>
..

</connector>

The resource adapter archive for the above OracleASjms.rar is :

  1. META-INF
    1. oc4j-ra.xml
    2. ra.xml
  2. grja.jar

In ra.xml, we have:

<connector>

<!– resourceadapter –>

<resourceadapter>
<resourceadapter-class>oracle.j2ee.ra.jms.generic.JMSResourceAdapter

</resourceadapter-class>
<config-property>
<config-property-name>lookupMethod</config-property-name>
<config-property-type>java.lang.String</config-property-type>
<config-property-value>resourceProvider</config-property-value>
</config-property>
<config-property>
<config-property-name>resourceProviderName</config-property-name>
<config-property-type>java.lang.String</config-property-type>
<config-property-value>oc4jjms</config-property-value>
</config-property>
..

..

</connector>

Using the configuration property in the oc4j-connectors.xml, the above connector in the resource adapter(ra.xml) is instantiated which will be the resource adapter object taking care of connecting to the JMS provider(in this case the oracle JMS, loop-back connection).

JMS – Java Messaging Service

September 5, 2006

JMS API allows applications to create, send, receive, and read messages.

When to choose JMS

  1. Provider wants the components not to depend on information about other components’ interfaces, so that components can be easily replaced.
  2. The provider wants the application to run whether or not all components are up and running simultaneously.
  3. The application business model allows a component to send information to another and to continue to operate without receiving an immediate response.

How JMS Provider fits with AS:

JMS Provider integrated with application server through the JCA. Access the JMS provider through the resource adapter. So, multiple JMS providers can be created by multiple vendors and can be fitted into different application servers.

In OC4J, default JMS Provider implementation class is com.evermind.server.jms.Oc4jResourceProvider exposed by name : oc4jjms(application.xml). This is a loop-back JMS provider. Set the additional configuration property(addressList) for connection to other machines.

<resource-provider class=”com.evermind.server.jms.Oc4jResourceProvider” name=”oc4jjms”>
</resource-provider>
This resource provider is accessed by AS components through JCA(configured in oc4j-connectors.xml)

<connector name=”OracleASjms” path=”OracleASjms.rar”>

<config-property name=”resourceProviderName” value=”oc4jjms”/>

..

</connector>

OracleASjms.rar is the resource adapter archive which will contain the resource adapter deployment descriptor.

From SUN tutorial : JMS Provider is accessed like : mq://sysname:7676/
This is connection implementation specific. Above is just an example
Thus, we have a provider exposing the service at some port.

Configuring JMS in Sun One AS

  1. Create Connection factory. Just choose type as javax.jms.ConnectionFactory and expose it through some JNDI name(like: jms/ConnectionFactory)
  2. Create Physical destinations(queue connection for point-to-point, topic for publish-subscribe).Select name as some valid name(MyPhysicalQueue or MyPhysicalTopic) and choose type as queue or topic.
  3. Create destination resource for the above destination. Expose this resource through a JNDI name for the clients.

Coding

  1. Create connection factory
  2. Create connection using connection factory
  3. Create session using connection
  4. Using session create producer passing the destination object as parameter
  5. Using producer send message.
  6. From the consumer side, create consumer passing the destination object.
  7. Call the connection.start to start the message delivery process
  8. use consumer.receive to receive message.
  9. Additionally create message listeners and register with consumer so that consumer will call the onMessage method of the consumer when message arrives. No need to code consumer.receive to receive and process message.

Configuration for setup JMS across systems

Earth is local system and Jupiter is remote system. Setup the following configuration:

  1. Create connection factory named jms/JupiterConnectionFactory in jupiter.
  2. create connection factory in earth with same name, but set the AddressList property for this connection factory to the name of remote system (like :mq://sysname:7676/e).
  3. Create queue in Jupiter and in earth with same name.
  4. Now, when earth creates connection, it will be a connection to remote system(jupiter) and hence message creation, session etc will take place in jupiter for process running in earth. queue creation in earth is for compilation purpose only as it doesn’t hold anything in this case. It is the queue of jupiter that is used as destination in this case.