EJB3: Session Beans, Example 3

by admin ~ October 14, 2008

Following is the example from the chapter “Session Beans” from the book “Enterprise JavaBeans 3.0″ by Bill Bruke & Richard Monson-Haefel. This example I have tried it out on JBoss AS 5.0.0.CR1. I have used Postgresql as the database. And I have used eclipse WTP as IDE.


Example 3: Annotationless Stateless Session Bean


This example is same as the above example 2, except that all annotations on the ProcessPayment EJB’s bean and interface classes have been removed. All meta-data is expressed in the ejb-jar.xml. So most of the classes and the configuration files are the same as the example 2, except the following:

ProcessPayment.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.titan.processpayment;
 
import com.titan.domain.*;
 
public interface ProcessPayment {
	public boolean byCheck(Customer customer, CheckDO check, double amount)
			throws PaymentException;
 
	public boolean byCash(Customer customer, double amount)
			throws PaymentException;
 
	public boolean byCredit(Customer customer, CreditCardDO card, double amount)
			throws PaymentException;
}

ProcessPaymentLocal.java

1
2
3
package com.titan.processpayment;
 
public interface ProcessPaymentLocal extends ProcessPayment {}

ProcessPaymentRemote.java

1
2
3
package com.titan.processpayment;
 
public interface ProcessPaymentRemote extends ProcessPayment {}

ProcessPaymentBean.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.titan.processpayment;
 
import com.titan.domain.*;
 
import java.sql.*;
import javax.sql.DataSource;
import javax.ejb.EJBException;
 
public class ProcessPaymentBean implements ProcessPaymentRemote,
		ProcessPaymentLocal {
 
	final public static String CASH = "CASH";
	final public static String CREDIT = "CREDIT";
	final public static String CHECK = "CHECK";
 
	DataSource dataSource;
 
	int minCheckNumber = 100;
 
	public boolean byCash(Customer customer, double amount)
			throws PaymentException {
		return process(customer.getId(), amount, CASH, null, -1, null, null);
	}
 
	public boolean byCheck(Customer customer, CheckDO check, double amount)
			throws PaymentException {
		if (check.checkNumber > minCheckNumber) {
			return process(customer.getId(), amount, CHECK, check.checkBarCode,
					check.checkNumber, null, null);
		} else {
			throw new PaymentException(
					"Check number is too low. Must be at least "
							+ minCheckNumber);
		}
	}
 
	public boolean byCredit(Customer customer, CreditCardDO card, double amount)
			throws PaymentException {
		if (card.expiration.before(new java.util.Date())) {
			throw new PaymentException("Expiration date has passed");
		} else {
			return process(customer.getId(), amount, CREDIT, null, -1,
					card.number, new java.sql.Date(card.expiration.getTime()));
		}
	}
 
	private boolean process(int customerID, double amount, String type,
			String checkBarCode, int checkNumber, String creditNumber,
			java.sql.Date creditExpDate) throws PaymentException {
 
		Connection con = null;
 
		PreparedStatement ps = null;
 
		try {
			con = dataSource.getConnection();
			ps = con
					.prepareStatement("INSERT INTO payment (customer_id, amount, type,"
							+ "check_bar_code,check_number,credit_number,"
							+ "credit_exp_date) VALUES (?,?,?,?,?,?,?)");
			ps.setInt(1, customerID);
			ps.setDouble(2, amount);
			ps.setString(3, type);
			ps.setString(4, checkBarCode);
			ps.setInt(5, checkNumber);
			ps.setString(6, creditNumber);
			ps.setDate(7, creditExpDate);
			int retVal = ps.executeUpdate();
			if (retVal != 1) {
				throw new EJBException("Payment insert failed");
			}
			return true;
		} catch (SQLException sql) {
			throw new EJBException(sql);
		} finally {
			try {
				if (ps != null)
					ps.close();
				if (con != null)
					con.close();
			} catch (SQLException se) {
				se.printStackTrace();
			}
		}
	}
}

ejb-jar.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?xml version="1.0"?>
<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>ProcessPaymentBean</ejb-name>
         <remote>com.titan.processpayment.ProcessPaymentRemote</remote>
         <local>com.titan.processpayment.ProcessPaymentLocal</local>
         <ejb-class>com.titan.processpayment.ProcessPaymentBean</ejb-class>
         <session-type>Stateless</session-type>
          <env-entry>
             <env-entry-name>min</env-entry-name>
             <env-entry-type>java.lang.Integer</env-entry-type>
             <env-entry-value>10</env-entry-value>
             <injection-target>
                <injection-target-class>
                   com.titan.processpayment.ProcessPaymentBean
                </injection-target-class>
                <injection-target-name>minCheckNumber</injection-target-name>
             </injection-target>
          </env-entry>
         <resource-ref>
             <res-ref-name>theDatasource</res-ref-name>
             <res-type>javax.sql.DataSource</res-type>
             <res-auth>Container</res-auth>
             <mapped-name>java:/TitanDB</mapped-name>
             <injection-target>
                <injection-target-class>
                   com.titan.processpayment.ProcessPaymentBean
                </injection-target-class>
                <injection-target-name>dataSource</injection-target-name>
             </injection-target>
          </resource-ref>
       </session>
   </enterprise-beans>
</ejb-jar>

build.xml

It is the same as the example 2.

Packaging Structure

It is the same as the example 2.

Server console after running the client

It is the same as the example 2.

Client console after running the client

It is the same as the example 2.

Download this example

http://ranjankumar.com/wp-content/plugins/downloads-manager/img/icons/winzip.gif download: Stateless Session Bean Example 3 (65.91KB)
added: 20/11/2008
clicks: 8
description: Annotationless

References:

“Java Persistence with Hibernate” by Christian Bauer and Gavin King.
“Enterprise JavaBeans 3.0″ by Bill Burke & Richard Monson-Haefel
JBoss Documentations for EJB 3

Share This Post

Leave a Reply