Skip to content

Commit

Permalink
Using Deployment Descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
ecme-mx committed Jul 29, 2016
1 parent 49d23c7 commit c973efa
Show file tree
Hide file tree
Showing 17 changed files with 197 additions and 21 deletions.
16 changes: 16 additions & 0 deletions backend/WebContent/WEB-INF/ejb-jar.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
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/ejb-jar_3_0.xsd"
version="3.0">
<enterprise-beans>
<session>
<ejb-name>CalculatorImpl</ejb-name>
<business-local>com.ecme.sessionbean.CalculatorLocal</business-local>
<business-remote>com.ecme.sessionbean.CalculatorRemote</business-remote>
<ejb-class>com.ecme.sessionbean.impl.CalculatorImpl</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index2.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
Expand Down
10 changes: 10 additions & 0 deletions backend/WebContent/index2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>EJB 3.1</title>
</head>
<body>
<h1>EJB 3.1 - Examples</h1>
</body>
</html>
6 changes: 6 additions & 0 deletions backend/src/com/ecme/sessionbean/CalculatorLocal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.ecme.sessionbean;

public interface CalculatorLocal {
public int sum(int add1, int add2);
public int multiply(int mul1, int mul2);
}
6 changes: 6 additions & 0 deletions backend/src/com/ecme/sessionbean/CalculatorRemote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.ecme.sessionbean;

public interface CalculatorRemote {
public int sum(int add1, int add2);
public int multiply(int mul1, int mul2);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
public interface ILibraryStatefulSessionBeanRemote {
void addBook(String bookName);
List<String> getBooks();
void remove();
void invalidateSession();
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.ecme.sessionbean;

import java.util.List;
import java.util.concurrent.Future;

import javax.ejb.Remote;


@Remote
public interface ILibraryStatelessSessionBeanRemote {
void addBook(String bookName);
Future<String> addBook(String bookName);
List<String> getBooks();
}
16 changes: 16 additions & 0 deletions backend/src/com/ecme/sessionbean/impl/CalculatorImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.ecme.sessionbean.impl;

import com.ecme.sessionbean.CalculatorLocal;
import com.ecme.sessionbean.CalculatorRemote;

public class CalculatorImpl implements CalculatorLocal, CalculatorRemote {

public int sum(int add1, int add2) {
return add1 + add2;
}

public int multiply(int mul1, int mul2) {
return mul1 * mul2;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void destroy(){
System.out.println("_______________@PreDestroy EXECUTED ___________________");
}
@Remove
public void remove(){
public void invalidateSession(){
System.out.println("_______________@Remove EXECUTED ___________________");
}
@PrePassivate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.AfterBegin;
import javax.ejb.AfterCompletion;
import javax.ejb.BeforeCompletion;
import javax.ejb.PostActivate;
import javax.ejb.PrePassivate;
import javax.ejb.Remove;
import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;

import com.ecme.sessionbean.ILibraryStatelessSessionBeanRemote;
Expand All @@ -23,10 +20,19 @@ public class LibraryStatelessSessionBean implements ILibraryStatelessSessionBean
public LibraryStatelessSessionBean(){
bookShelf = new ArrayList<String>();
}

public void addBook(String bookName) {
@Asynchronous
public Future<String> addBook(String bookName) {
System.out.println("!!!!!Agregando libro stateless:"+bookName+" from thread: "+Thread.currentThread().getName());
bookShelf.add(bookName);
System.out.println("!!!!!Agregando libro stateless:"+bookName);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Agregado ----------> "+bookName);
return new AsyncResult<String>("RESULT_"+bookName+Thread.currentThread().getName());

}

public List<String> getBooks() {
Expand Down
5 changes: 0 additions & 5 deletions backend/webapp/index.jsp

This file was deleted.

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

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

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

import com.ecme.sessionbean.CalculatorRemote;

public class CalculatorClient {

BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;
{
props = new Properties();
InputStream input = null;
try {
String filename = "jndi.properties";
input = CalculatorClient.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("Calculator");
System.out.println("**********************");
System.out.print("Options \n1. Sum\n2. Multiply \n3. Exit\nEnter Choice: ");
}

private void testStatelessEjb(){

try {
int choice = 1;
int a = 0;
int b = 0;

CalculatorRemote calculatorRemote =
(CalculatorRemote)ctx.lookup("java:global/ecme-ejb31/CalculatorImpl!com.ecme.sessionbean.CalculatorRemote");

while (choice != 3) {
showGUI();
String strChoice = brConsoleReader.readLine();
choice = Integer.parseInt(strChoice);

if (choice == 1) {
System.out.println("Value for a:");
String strA = brConsoleReader.readLine();
System.out.println("Value for b:");
String strB = brConsoleReader.readLine();
a = Integer.parseInt(strA);
b = Integer.parseInt(strB);

System.out.println("sum: "+calculatorRemote.sum(a,b));
} else if (choice == 2) {
System.out.println("Value for a:");
String strA = brConsoleReader.readLine();
System.out.println("Value for b:");
String strB = brConsoleReader.readLine();
a = Integer.parseInt(strA);
b = Integer.parseInt(strB);

System.out.println("multiply: "+calculatorRemote.multiply(a,b));

} else if (choice == 3) {
break;
}
}

} 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
CalculatorClient client = new CalculatorClient();
client.testStatelessEjb();
}

}

4 changes: 4 additions & 0 deletions client/src/main/java/com/ecme/app/StatefulClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ private void testStatelessEjb(){
}
}
System.out.println("Listando libros ");

libraryBean.invalidateSession();

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

System.out.println("Book(s) entered so far: " + booksList.size());
Expand All @@ -89,6 +92,7 @@ private void testStatelessEjb(){
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();
Expand Down
5 changes: 4 additions & 1 deletion client/src/main/java/com/ecme/app/StatelessClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Future;

import javax.naming.InitialContext;
import javax.naming.NamingException;
Expand Down Expand Up @@ -66,7 +67,9 @@ private void testStatelessEjb(){
System.out.print("Enter book name: ");
bookName = brConsoleReader.readLine();

libraryBean.addBook(bookName);
Future<String> response= libraryBean.addBook(bookName);
Thread.sleep(4000);
System.out.print("Esperando respuesta .... "+response.get());
} else if (choice == 2) {
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.ecme.sessionbean;

public interface CalculatorRemote {
public int sum(int add1, int add2);
public int multiply(int mul1, int mul2);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
public interface ILibraryStatefulSessionBeanRemote {
void addBook(String bookName);
List<String> getBooks();
void remove();
void invalidateSession();

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

import java.util.List;
import java.util.concurrent.Future;

import javax.ejb.Remote;


@Remote
public interface ILibraryStatelessSessionBeanRemote {
void addBook(String bookName);
Future<String> addBook(String bookName);
List<String> getBooks();
}

0 comments on commit c973efa

Please sign in to comment.