Archive

Archive for the ‘EJB3’ Category

EJB3 Sample application in Weblogic 10.3

February 19, 2013 2 comments

In this post will see how to create and test a EJB3 SayHello application.
Here I have created a simple stateless session bean and packaged it as EAR using ANT script.

Jars required :
1. javaee-api-5.0-2.jar
2. wlfullclient.jar

EAR Structure :

ApplicationStructure

ApplicationStructure


 

SayHello Sample Structure

SayHelloEclipseStructure

SayHelloEclipseStructure


 

SayHello.java remote interface

package com.wordpress.rathinasaba.ejb3;

import javax.ejb.Remote;

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

 
SayHelloBean.java
This Stateless session bean implements Remote interface SayHello.java
name
mappedName
description
the above three attributes can be used in Stateless session bean.

Here the mappedName attribute is used from Client

package com.wordpress.rathinasaba.ejb3;

import javax.ejb.Stateless;

@Stateless(mappedName = "SayHello")
public class SayHelloBean implements SayHello {
	
	public String sayHello(String name) {
		return "Hello " + name + " welcome to EJB 3 In Action!";
	}
}

 

application.xml

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:application="http://java.sun.com/xml/ns/javaee/application_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd"
	id="Application_ID" version="5">
	<display-name>SayHello EJB3 Sample</display-name>
	<module>
		<ejb>SayHello-ejb.jar</ejb>
	</module>
</application>

 

weblogic-application.xml

<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-application xmlns:wls="http://www.bea.com/ns/weblogic/weblogic-application"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/javaee_5.xsd http://www.bea.com/ns/weblogic/weblogic-application http://www.bea.com/ns/weblogic/weblogic-application/1.0/weblogic-application.xsd">
	<wls:application-param>
		<wls:param-name>webapp.encoding.default</wls:param-name>
		<wls:param-value>UTF-8</wls:param-value>
	</wls:application-param>
</wls:weblogic-application>

 

build.properties

wls.home=C:\\Saba\\soa11g\\soa11.1.1.5\\Middleware

ANT Script – build.xml
The below ANT script file will compile the EJB3 Code and pack it as SayHello.ear file.

<?xml version="1.0" encoding="UTF-8"?>
<project name="SayHello-ejb" default="buildEar" basedir=".">
	<property file="build.properties" />
	<property name="ear.name" value="SayHello" />
	<property name="src.server" value="${basedir}/src/server" />
	<property name="src.common" value="${basedir}/src/common" />
	<property name="build" value="${basedir}/build" />
	<property name="dist" value="${basedir}/dist" />
	<property name="wl.home.lib" value="${wls.home}/wlserver_10.3/server/lib" />

	<echo message="${wl.home.lib}" />

	<path id="classpath">
		<fileset dir="${wl.home.lib}">
			<include name="*.jar" />
		</fileset>
	</path>

	<target name="clean">
		<delete dir="${build}" />
		<delete dir="${dist}" />
	</target>

	<target name="init" depends="clean">
		<mkdir dir="${build}" />
		<mkdir dir="${dist}/META-INF" />
		<mkdir dir="${dist}/APP-INF/lib" />
		<mkdir dir="${dist}/APP-INF/classes" />
		<mkdir dir="${dist}" />
		
	</target>

	<taskdef name="wlappc" classpathref="classpath" classname="weblogic.ant.taskdefs.j2ee.Appc" />

	<target name="compile" depends="init">
		<echo> "Compiling Java" </echo>
		<javac srcdir="${src.server}" destdir="${build}" debug="on">
			<classpath refid="classpath" />
		</javac>
	</target>

	<target name="ejb3Compile" depends="compile">
		<echo> "Compiling EJB" </echo>
		<wlappc source="${build}" output="${build}" keepgenerated="true" verbose="true"/>
	</target>
	
	<target name="buildEar" depends="ejb3Compile">
		<echo> "Building EJB JAR" </echo>
		<copydir dest="${dist}/META-INF/" src="${src.common}/META-INF/"/>
		<jar jarfile="${dist}/${ant.project.name}.jar" basedir="${build}" />
		<jar jarfile="${dist}/${ear.name}.ear" basedir="${dist}" />
	</target>
	
</project>

 
Deployment and Client Testing
1. Deploy the SayHello.ear in weblogic server by targetting managed server.
2. Use the below Client Code to Test the EJB3 Sample from main method.

SayHelloClient.java

package com.wordpress.rathinasaba.ejb3.client;

import java.util.Hashtable;

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

import com.wordpress.rathinasaba.ejb3.SayHello;

public class SayHelloClient {

	private static SayHello client;

	public static void main(String[] args) {
		println(invokeEJB3());
	}

	public static String invokeEJB3() {
		println("===============================================");
		println("                Invoking EJB                   ");
		println("===============================================");
		String response = null;
		try {
			Context ctx = new InitialContext(getInitialContext());
			client = (SayHello) ctx.lookup("SayHello#com.wordpress.rathinasaba.ejb3.SayHello");
			response = client.sayHello("Rathina Sabapathy");
			println("--- Invoked EJB successfully ---");
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return response;
	}

	private static void println(Object content) {
		System.out.println(content);
	}

	private static Hashtable<String, Object> getInitialContext() {
		Hashtable<String, Object> props = new Hashtable<String, Object>();
		props.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
		props.put(Context.PROVIDER_URL, "t3://localhost:8011");
		return props;
	}

}
Advertisements
Categories: EJB3 Tags: