Step 1 :
Download jboss 5.1 application server from below (Which supporting for JDK 1.6)
http://jbossas.jboss.org/downloads
Extract and start the Jboss server.
Step 2 :
Create sample maven project as below
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ejb</groupId>
<artifactId>ejb-stateless</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>ejb-api</artifactId>
<version>3.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.jbossas</groupId>
<artifactId>jboss-as-client</artifactId>
<version>5.1.0.GA</version>
<type>pom</type>
</dependency>
</dependencies>
</project>
Step 3:
Create Stateless bean as remote.
package ejb.statless;
import javax.ejb.*;
import java.util.*;
@Remote
public interface LibrarySessionBeanRemote {
void addBook(String bookName);
List<String> getBooks();
}
package ejb.statless;
import javax.ejb.*;
import java.util.*;
@Stateless
public class LibrarySessionBean implements LibrarySessionBeanRemote {
List<String> bookShell = new ArrayList<String>();
public LibrarySessionBean() {
}
@Override
public void addBook(String bookName) {
System.out.println(bookShell + "book shell....................");
System.out.println(bookName + "book name....................");
bookShell.add(bookName);
}
@Override
public List<String> getBooks() {
return bookShell;
}
}
Step 4: Build the project and upload jar to Jboss server EJB3 container.
Step 5: Create sample client
package ejb.statless;
import javax.naming.*;
import java.io.*;
import java.util.*;
public class EJBTestClient {
BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;
{
/* props = new Properties();
try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}*/
Hashtable hashtable = new Hashtable();
hashtable.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
hashtable.put(Context.PROVIDER_URL, "jnp://localhost:1099");
hashtable.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
try {
ctx = new InitialContext(hashtable);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader = new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTestClient ejbTester = new EJBTestClient();
ejbTester.testStatelessEjb();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options \n1. Add Book\n2. Exit \nEnter Choice: ");
}
private void testStatelessEjb(){
try {
int choice = 1;
LibrarySessionBeanRemote libraryBean = (LibrarySessionBeanRemote) ctx.lookup ("LibrarySessionBean/remote");
while (true) {
String bookName;
showGUI();
String strChoice = brConsoleReader.readLine();
choice = Integer.parseInt(strChoice);
if (choice == 1) {
System.out.print("Enter book name: ");
bookName = brConsoleReader.readLine();
assert libraryBean != null;
libraryBean.addBook(bookName);
} else if (choice == 2) {
break;
}
}
List<String> booksList = libraryBean.getBooks();
System.out.println("Book(s) entered so far: " + booksList.size());
int i = 0;
for (String book:booksList) {
System.out.println((i+1)+". " + book);
i++;
}
LibrarySessionBeanRemote libraryBean1 =
(LibrarySessionBeanRemote)ctx.lookup("LibrarySessionBean/remote");
List<String> booksList1 = libraryBean1.getBooks();
System.out.println(
"Book(s) entered so far: " + booksList1.size());
for (i = 0; i < booksList1.size(); ++i) {
System.out.println((i+1)+". " + booksList1.get(i));
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}finally {
try {
if(brConsoleReader !=null){
brConsoleReader.close();
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
}
Run main method and check the result :)
Sample code : git clone https://github.com/malithmeee/jboss-ejb3.git
Download jboss 5.1 application server from below (Which supporting for JDK 1.6)
http://jbossas.jboss.org/downloads
Extract and start the Jboss server.
Step 2 :
Create sample maven project as below
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ejb</groupId>
<artifactId>ejb-stateless</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>ejb-api</artifactId>
<version>3.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.jbossas</groupId>
<artifactId>jboss-as-client</artifactId>
<version>5.1.0.GA</version>
<type>pom</type>
</dependency>
</dependencies>
</project>
Step 3:
Create Stateless bean as remote.
package ejb.statless;
import javax.ejb.*;
import java.util.*;
@Remote
public interface LibrarySessionBeanRemote {
void addBook(String bookName);
List<String> getBooks();
}
package ejb.statless;
import javax.ejb.*;
import java.util.*;
@Stateless
public class LibrarySessionBean implements LibrarySessionBeanRemote {
List<String> bookShell = new ArrayList<String>();
public LibrarySessionBean() {
}
@Override
public void addBook(String bookName) {
System.out.println(bookShell + "book shell....................");
System.out.println(bookName + "book name....................");
bookShell.add(bookName);
}
@Override
public List<String> getBooks() {
return bookShell;
}
}
Step 4: Build the project and upload jar to Jboss server EJB3 container.
Step 5: Create sample client
package ejb.statless;
import javax.naming.*;
import java.io.*;
import java.util.*;
public class EJBTestClient {
BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;
{
/* props = new Properties();
try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}*/
Hashtable hashtable = new Hashtable();
hashtable.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
hashtable.put(Context.PROVIDER_URL, "jnp://localhost:1099");
hashtable.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
try {
ctx = new InitialContext(hashtable);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader = new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTestClient ejbTester = new EJBTestClient();
ejbTester.testStatelessEjb();
}
private void showGUI(){
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options \n1. Add Book\n2. Exit \nEnter Choice: ");
}
private void testStatelessEjb(){
try {
int choice = 1;
LibrarySessionBeanRemote libraryBean = (LibrarySessionBeanRemote) ctx.lookup ("LibrarySessionBean/remote");
while (true) {
String bookName;
showGUI();
String strChoice = brConsoleReader.readLine();
choice = Integer.parseInt(strChoice);
if (choice == 1) {
System.out.print("Enter book name: ");
bookName = brConsoleReader.readLine();
assert libraryBean != null;
libraryBean.addBook(bookName);
} else if (choice == 2) {
break;
}
}
List<String> booksList = libraryBean.getBooks();
System.out.println("Book(s) entered so far: " + booksList.size());
int i = 0;
for (String book:booksList) {
System.out.println((i+1)+". " + book);
i++;
}
LibrarySessionBeanRemote libraryBean1 =
(LibrarySessionBeanRemote)ctx.lookup("LibrarySessionBean/remote");
List<String> booksList1 = libraryBean1.getBooks();
System.out.println(
"Book(s) entered so far: " + booksList1.size());
for (i = 0; i < booksList1.size(); ++i) {
System.out.println((i+1)+". " + booksList1.get(i));
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}finally {
try {
if(brConsoleReader !=null){
brConsoleReader.close();
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
}
Run main method and check the result :)
Sample code : git clone https://github.com/malithmeee/jboss-ejb3.git
Comments
Post a Comment