Skip to content

Commit

Permalink
add client for stateful session bean
Browse files Browse the repository at this point in the history
  • Loading branch information
ecme-mx committed Jul 21, 2016
1 parent d06036b commit 13089df
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 0 deletions.
1 change: 1 addition & 0 deletions client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
7 changes: 7 additions & 0 deletions client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<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>com.ecme.client</groupId>
<artifactId>ecme-ejb-client</artifactId>
<version>0.0.1-SNAPSHOT</version>

</project>
123 changes: 123 additions & 0 deletions client/src/main/java/com/ecme/app/StatefulClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.ecme.app;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;

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

import com.ecme.sessionbean.ILibraryStatefulSessionBeanRemote;

public class StatefulClient {

BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;
{
props = new Properties();
InputStream input = null;
try {
String filename = "jndi.properties";
input = StatefulClient.class.getClassLoader().getResourceAsStream(filename);
if(input==null){
System.out.println("Sorry, unable to find " + filename);
}
props.load(input);
} catch (IOException ex) {
ex.printStackTrace();
}
try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
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;

ILibraryStatefulSessionBeanRemote libraryBean =
(ILibraryStatefulSessionBeanRemote)ctx.lookup("java:global/ecme-ejb31/LibraryStatefulSessionBean!com.ecme.sessionbean.ILibraryStatefulSessionBeanRemote");

while (choice != 2) {
String bookName;
showGUI();
String strChoice = brConsoleReader.readLine();
choice = Integer.parseInt(strChoice);
if (choice == 1) {
System.out.print("Enter book name: ");
bookName = brConsoleReader.readLine();
Book book = new Book();
book.setName(bookName);
libraryBean.addBook(book.getName());
} else if (choice == 2) {
break;
}
}

List<Book> booksList = libraryBean.getBooks();

System.out.println("Book(s) entered so far: " + booksList.size());
int i = 0;
for (Book book:booksList) {
System.out.println((i+1)+". " + book.getName());
i++;
}
ILibraryStatefulSessionBeanRemote libraryBean1 =
(ILibraryStatefulSessionBeanRemote)ctx.lookup("LibraryStatefulSessionBean/remote");
List<String> booksList1 = libraryBean1.getBooks();
System.out.println(
"***Using second lookup to get library stateful object***");
System.out.println(
"Book(s) entered so far: " + booksList1.size());
for (int j = 0; j < booksList1.size(); ++j) {
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());
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
StatefulClient client = new StatefulClient();
client.testStatelessEjb();
}

}
class Book{
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.ecme.sessionbean;

import java.util.List;


public interface ILibraryStatefulSessionBeanRemote {
void addBook(String bookName);
List getBooks();
}
4 changes: 4 additions & 0 deletions client/src/main/resources/jndi.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
java.naming.factory.initial=com.sun.enterprise.naming.SerialInitContextFactory
java.naming.factory.url.pkgs=com.sun.enterprise.naming
org.omg.CORBA.ORBInitialHost=localhost
java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl
12 changes: 12 additions & 0 deletions src/com/ecme/sessionbean/ILibraryStatefulSessionBeanRemote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.ecme.sessionbean;

import java.util.List;

import javax.ejb.Remote;


@Remote
public interface ILibraryStatefulSessionBeanRemote {
void addBook(String bookName);
List getBooks();
}
2 changes: 2 additions & 0 deletions src/com/ecme/sessionbean/IStatefulSessionBean.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.ecme.sessionbean;

import javax.ejb.Remote;

@Remote
public interface IStatefulSessionBean {
public String sayHello(String value);

Expand Down
27 changes: 27 additions & 0 deletions src/com/ecme/sessionbean/impl/LibraryStatefulSessionBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.ecme.sessionbean.impl;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateful;

import com.ecme.sessionbean.ILibraryStatefulSessionBeanRemote;

@Stateful
public class LibraryStatefulSessionBean implements ILibraryStatefulSessionBeanRemote {

List<String> bookShelf;

public LibraryStatefulSessionBean(){
bookShelf = new ArrayList<String>();
}

public void addBook(String bookName) {
bookShelf.add(bookName);
}

public List<String> getBooks() {
return bookShelf;
}

}

0 comments on commit 13089df

Please sign in to comment.