-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add client for stateful session bean
- Loading branch information
Showing
8 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |
9 changes: 9 additions & 0 deletions
9
client/src/main/java/com/ecme/sessionbean/ILibraryStatefulSessionBeanRemote.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
src/com/ecme/sessionbean/ILibraryStatefulSessionBeanRemote.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/com/ecme/sessionbean/impl/LibraryStatefulSessionBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |