Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
Enterprise Java in a Nutshell
Kyle BrownKyle BrownSenior Java ConsultantSenior Java ConsultantIBM WebSphere IBM WebSphere ServicesServicesRTP, NCRTP, [email protected]@us.ibm.com
1
© Copyright IBM Corporation 1999
Overview
:KDW�LV�(QWHUSULVH�-DYD"
-DYD�DQG�WKH�:HE6HUYOHWV�DQG�-63V
(QWHUSULVH�-DYD�%HDQV
$UFKLWHFWXUHV�IRU�(QWHUSULVH�-DYD
2
© Copyright IBM Corporation 1999
What is Enterprise Java?
$�VHW�RI�$3,V�IRU�6HUYHU�VLGH�-DYD�3URJUDPPLQJ-DYD�6HUYOHWV��-DYD�6HUYHU�3DJHV(QWHUSULVH�-DYD�%HDQV��(-%V�-DYD�0HVVDJH�6HUYLFH��-06�2WKHUV
$�GLUHFW�FRPSHWLWRU�WR�0LFURVRIWV�'1$
3
© Copyright IBM Corporation 1999
Enterprise Java and MVC
7KH�09&�3DWWHUQ�LV�DW�WKH�KHDUW�RI�(QWHUSULVH�-DYD�0RGHO����5HSUHVHQW�WKH�SUREOHP�GRPDLQ9LHZ����5HSUHVHQW�D�ZD\�RI�LQWHUDFWLQJ�ZLWK�WKH�PRGHO&RQWUROOHU����0HGLDWH�EHWZHHQ�WKH�WZR��DQG�PDQDJH�DSSOLFDWLRQ�IORZ
&OHDQO\�VHSDUDWHV�SUHVHQWDWLRQ��9LHZ��FRGH�IURP�FRQWHQW��0RGHO��FRGH
4
© Copyright IBM Corporation 1999
Separating Presentation and Content
Business Objects(Model)
Servlets(Interaction Controllers)
Input Page(View)
Display Page(View)
JSP(view)
12
3
4
5View
Server
5
This corresponds to specific pieces of Enterprise Java:
(1) The Model is represented by Java Beans and Enterprise Java beans. It is implemented with Java code that may use IBM’s enterprise connectors.(2) The View is implemented using Java Server Pages (JSP’s)(3) The Control logic is implemented as Java Servlets
Each layer may be implemented by different people, giving a better separation of roles. There are consistent interfaces between the layers.
© Copyright IBM Corporation 1999
A Server-side Java ArchitectureWeb Browser
Browser + AppletApplication
DB
Java Servlets
Java Server Pages
Enterprise Java
BeansEJB
Services
IIOPJD
BC
, JM
SIIO
P
HT
TP
Client Tier
2nd Tier Servers
Third Tier Servers
6
© Copyright IBM Corporation 1999
Java and the Web
-DYD�6HUYOHWV�DUH�VPDOO��VHUYHU�VLGH�-DYD�SURJUDPV�WKDW�UHVSRQG��W\SLFDOO\��WR�FOLHQW�+773�UHTXHVWVVLPLODU�WR�D�&*,�SURJUDP
-DYD�6HUYHU�3DJHV�DUH�D�ZD\�IRU�HPEHGGLQJ�-DYD�FRGH�LQWR�VHUYHU�VLGH�+70/�SDJHV$�WHPSODWH�ODQJXDJH�IRU�-DYD�%HDQV�DQG�VHUYHU�VLGH�+70/�SURFHVVLQJ
7
© Copyright IBM Corporation 1999
Java Servlets
$�-DYD�FODVV�WKDW�UHSUHVHQWV�D�VLQJOH�85/�WR�WKH�FOLHQW'HILQHV�D�VHUYLFH���PHWKRG�WKDW�KDQGOHV�+773�UHTXHVWV+WWS6HUYOHW5HTXHVW����DFFHVV�UHTXHVW�GDWD+WWS6HUYOHW5HVSRQVH�����UHSO\�WR�WKH�FOLHQW
$Q�LQVWDQFH�RI�HDFK�FODVV�LV�D�VKDUHG�UHVRXUFH�XVHG�E\�PXOWLSOH�WKUHDGV(DFK�WKUHDG�KDQGOHV�DQ�+773�UHTXHVW�
8
© Copyright IBM Corporation 1999
Servlet Lifecycle
7KH�LQLW���PHWKRG�LV�FDOOHG�DW�ORDG�WLPH�RQH�WLPH�EHKDYLRU
7KH�VHUYLFH���PHWKRG�LV�LQYRNHG�IRU�HDFK�FOLHQW�UHTXHVW
7KH�GHVWUR\���PHWKRG�LV�FDOOHG�ZKHQ�LW�LV�XQORDGHG
init()
service()
destroy()
9
One additional thing to remember is that there is only ONE instance of each servlet class. Multiple threads invoke the service() method in the same instance. This is a key point that has some ramifications; the biggest being that servlets should be stateless. The state of your application is stored outside of your servlet (more on that later).
One exception to the above: A servlet can implement javax.servlet.SingleThreadModel. This forces the servlet to only have a single thread active within it at any time. This is usually implemented (by the servlet runtime) by providing a pool of servlet instances with each request acquiring exclusive access to one instance for the duration of the service method.
© Copyright IBM Corporation 1999
Simple Servlet
public class MyServlet extends HttpServlet { public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
// get stream to output HTML on! res.setContentType("text/html"); PrintWriter out = res.getWriter();
// send out a simple banner out.println("<HTML><BODY>"); out.println("<h1>Hello World!</h1>");
out.println("</BODY></HTML>"); }}
10
This is the simplest servlet. It just returns some HTML to the requestor. However, the three parts:(1) Set the content type(2) Get a PrintWriter(3) Output on the PrintWriterare seen in even the most complicated servlets.
© Copyright IBM Corporation 1999
HttpSession
$VN�IRU�D�6HVVLRQ�IURP�D�+WWS5HTXHVWDQ+WWS5HTXHVW�JHW6HVVLRQ�ERROHDQ�FUHDWH�
5HWXUQV�DQ�+WWS6HVVLRQ,I�FUHDWH�LV�IDOVH��XVH�D�SUHYLRXVO\�FUHDWHG�VHVVLRQ
+WWS6HVVLRQV�VWRUH�DSSOLFDWLRQ�VSHFLILF�LQIRUPDWLRQYRLG�SXW9DOXH�6WULQJ��2EMHFW�2EMHFW�JHW9DOXH�6WULQJ�
11
© Copyright IBM Corporation 1999
WebSphere Session Storage
Browser id value12345 aSession
aSession
key value"Customer" aCustomer"Name" "Bob"
cookie name value
"sesessionid" 12345
Cookie List
Application Server
12
© Copyright IBM Corporation 1999
Java Server Pages
6HUYOHW�DQG��EDFN�HQG��VXSSO\�G\QDPLF�FRQWHQW�LQ�D�-DYD%HDQ-63�DFFHVVHV�REMHFW�YLD��%($1!�WDJ�%($1!�WDJ�VSHFLILHV�KRZ�WR�ILQG�RU�FUHDWH�D�%HDQ
$�%HDQ�FDQ�EH�LQVWDQWLDWHG�IURP�VHULDOL]HG�ILOH�RU�FODVV�ILOH�UHWULHYHG�IURP�+WWS6HVVLRQUHWULHYHG�IURP�6HUYOHW5HTXHVW�FRQWH[W
13
© Copyright IBM Corporation 1999
JSP Sample
�+70/!�+($'!������+($'!�EHDQ�QDPH �XVU���W\SH �8VHU,QIR��LQWURVSHFW �QR��FUHDWH �QR��VFRSH �VHVVLRQ�!��EHDQ!�%2'<!����,I�WKLV�ZHUH�D�UHDO�DSSOLFDWLRQ��\RX�ZRXOG�FRQILUP�\RXUDGGUHVV�EHORZ�DQG�SURYLGH�ELOOLQJ�LQIRUPDWLRQWR�ILQDOL]H�\RXU�WUDQVDFWLRQ��S!�SUH!�� �XVU�JHW)LUVWQDPH����!��� �XVU�JHW/DVWQDPH����!�� �XVU�JHW6WUHHW����!�� �XVU�JHW&LW\����!���� �XVU�JHW6WDWH����!��� �XVU�JHW=LS����!��SUH!�����%2'<!��+70/!
14
© Copyright IBM Corporation 1999
Enterprise Java Beans
$�FRPSRQHQW�PRGHO�IRU�GHYHORSLQJ�DQG�GHSOR\LQJ�REMHFW�RULHQWHG��PXOWLWLHU�-DYD�DSSOLFDWLRQV%DVHG�RQ�WKUHH�IRXQGDWLRQDO�WHFKQRORJLHV
OMG’s CORBARMI (Java Remote Message Invocation)X/Open XA Transaction architecture
15
© Copyright IBM Corporation 1999
RMI Over IIOP
50,�RYHU�,,23�FRPELQHV�VRPH�RI�WKH�EHVW�SDUWV�RI�&25%$�DQG�50,
6LPSOH�SURJUDPPLQJ�PRGHO
,QWHURSHUDELOLW\�ZLWK�RWKHU�ODQJXDJHV
7KH�SUHIHUUHG�WHFKQRORJ\�IRU�(-%�GLVWULEXWLRQ
Client Program
Standard RMI Engine
ORB
IIOP
TCP/IP
Generated RMI Code
16
© Copyright IBM Corporation 1999
XA Transactions
Application/EJB Server
Transaction Manager
Resource ManagerResource
Manager
SQL, etc.
OTS TX
XA
JDBC 2.0JMS
17
© Copyright IBM Corporation 1999
So what’s an EJB?
$Q�(-%�LV�D�GLVWULEXWHG��WUDQVDFWLRQDO���SRVVLEO\��SHUVLVWHQW�VRIWZDUH�FRPSRQHQW3URJUDPPHU�SURYLGHV�D�VSHFLILFDWLRQ�DQG�LPSOHPHQWDWLRQ�IRU�LWV�EXVLQHVV�PHWKRGV7UDQVDFWLRQ�DWWULEXWHV�DQG�SHUVLVWHQFH�FDQ�EH�SURYLGHG�DW�'HSOR\PHQW�WLPH
18
© Copyright IBM Corporation 1999
(-%�6HUYHUV6HUYHU�VRIWZDUH�DQDORJRXV�WR�&25%$�25%�SURYLGHV�QDPLQJ�DQG�WUDQVDFWLRQ�VHUYLFHVKRVWV�FRQWDLQHUV
(-%�&RQWDLQHUV,QWHUIDFH�EHWZHHQ�(-%�%HDQ�DQG�RXWVLGH�ZRUOG+ROG�DQG�PDQDJH�EHDQV&RQWDLQHU�SURYLGHV��JHQHUDWHV��WKH�FODVVHV�IRU�GLVWULEXWLRQ�DQG�SHUVLVWHQFH+DQGOH�GHSOR\PHQW�SURFHVV
EJB Server Software
19
© Copyright IBM Corporation 1999
CORBA Service EJB Equivalent Explanation
Naming JNDI + EJB Name service*
Associate names with objects on the network
Lifecycle EJB Homes + Distributed GC
Manage objects with Object Factories
Security EJB Security Provide secured access to networked objects
Persistence Entity Beans Provide a standard way for objects to save themselves
Transaction EJB Transactions** Propagate transactions across object resources
EJB services and CORBA Services
20
© Copyright IBM Corporation 1999
EJB Objects
(-%�&OLHQWV�GR�QRW�GLUHFWO\�FRPPXQLFDWH�ZLWK�DQ�(-%
7KH�&RQWDLQHU�LQWHUVSHUVHV�DQ�(-%2EMHFW�EHWZHHQ�WKH�EHDQ�DQG�WKH�FOLHQW�VWXE
7KH�(-%2EMHFW�KDQGOHV�SHUVLVWHQFH��PXOWL�WKUHDGLQJ�DQG�WUDQVDFWLRQ�VXSSRUW
21
© Copyright IBM Corporation 1999
Client
EJB Server/Container
Home
EJBObject
Bean
HomeStub
EJBObjectStub
Bean Data
EJB Architecture
22
This shows all of the pieces of the EJB architecture. All that you get on the clent side is a stub (a proxy) to an actual object on the server side. The Home is what allows you to create and find EJB’s. The Beans themselves exist only on the server. An EJB Object is a container-provided object that handles things like:
*persistence in CMP entity beans*distribution details (CORBA, RMI)
The bean itself only implements business methods -- the framework handles the rest!
© Copyright IBM Corporation 1999
EJB Homes
$Q�(-%�+RPH�LV�D�)DFWRU\�FODVV,PSOHPHQWDWLRQ�RI�WKH�)DFWRU\�SDWWHUQ*HQHUDWHG�E\�WKH�FRQWDLQHU�IURP�WKH�+RPH�,QWHUIDFH&UHDWHV�DQG�RU�ILQGV�(-%V��KDQGOHV�OLIHF\FOH�LVVXHV�
23
© Copyright IBM Corporation 1999
EJB Clients
/RFDWH�(-%�+RPHV�YLD�-1',��-DYD�1DPLQJ�DQG�'LUHFWRU\�6HUYLFH�0D\�EH�6HUYOHWV��$SSOHWV�RU�$SSOLFDWLRQV
$FFHVV�LV�WKURXJK�JHQHUDWHG�FODVVHV
0DNHV�XVH�RI�(-%�FOLHQW�VLGH�VWXEV&OLHQWV�FRPPXQLFDWH�WR�(-%V�WKURXJK�VWDQGDUG�SURWRFROV��50,�RYHU�,,23�,PSOHPHQWDWLRQ�RI�WKH�3UR[\�3DWWHUQ
24
© Copyright IBM Corporation 1999
Bean Types
6HVVLRQ�EHDQV�DVVRFLDWHG�ZLWK�D�SDUWLFXODU�FOLHQW�FUHDWHG�DQG�GHVWUR\HG�E\�D�FOLHQW��WUDQVLHQW�GR�QRW�VXUYLYH�V\VWHP�VKXWGRZQ
(QWLW\�EHDQVVKDUHG�E\�PXOWLSOH�FOLHQWV�SHUVLVW�DFURVV�PXOWLSOH�LQYRFDWLRQV�VXUYLYH�V\VWHP�VKXWGRZQ
25
© Copyright IBM Corporation 1999
EJB Types
EnterpriseJava
Beans
Entity Beans
Session Beans
ContainerManaged
BeanManaged
Stateless Stateful
26
© Copyright IBM Corporation 1999
Stateless vs. Stateful Session Beans
6WDWHOHVV�QR�LQWHUQDO�VWDWH�GR�QRW�QHHG�WR�EH�SDVVLYDWHG�FDQ�EH�SRROHG�WR�VHUYLFH�PXOWLSOH�FOLHQWV
6WDWHIXO�SRVVHVV�LQWHUQDO�VWDWH�QHHG�WR�KDQGOH�SDVVLYDWLRQ�DFWLYDWLRQ�RQH�SHU�FOLHQW
27
© Copyright IBM Corporation 1999
Entity Bean persistence
&RQWDLQHU�PDQDJHG�&RQWDLQHU�LV�UHVSRQVLEOH�IRU�VDYLQJ�VWDWH�,Q�GHSOR\PHQW�GHVFULSWRU��VSHFLI\�FRQWDLQHU�PDQDJHG�ILHOGV�SHUVLVWHQFH�LQGHSHQGHQW�RI�GDWD�VRXUFH�
%HDQ�PDQDJHG�%HDQ�LV�UHVSRQVLEOH�IRU�VDYLQJ�LWV�RZQ�VWDWH�&RQWDLQHU�GRHVQ·W�QHHG�WR�JHQHUDWH�'%�FDOOV�/HVV�DGDSWDEOH��SHUVLVWHQFH�LV�KDUG�FRGHG
28
© Copyright IBM Corporation 1999
EJB Implementation
%XLOGLQJ�DQ�(-%�FRQVLVWV�RI�EXLOGLQJ�WKUHH�SDUWV+RPH�,QWHUIDFH����GHILQHV�OLIHF\FOH�PHWKRGV5HPRWH�,QWHUIDFH����GHILQHV�UHPRWH�PHWKRGV%HDQ�LPSOHPHQWDWLRQ�FODVV
$IWHU�EXLOGLQJ�WKHVH�\RX�GHSOR\�WKH�(-%7KH�FRQWDLQHU�JHQHUDWHV�WKH�+RPH��WKH�VWXEV��WKH�VNHOHWRQ��DQG�WKH�(-%�2EMHFW
29
© Copyright IBM Corporation 1999
EJB Deployment
7KH�'HSOR\PHQW�'HVFULSWRU�LV�DV�LPSRUWDQW�DV�WKH�FODVVHV
,W�LV�D�VHULDOL]HG�LQVWDQFH�RI�6HVVLRQ'HVFULSWRU�RU�(QWLW\'HVFULSWRU
3URYLGHV�LQIRUPDWLRQ�DERXW6WDWHIXO�YV��6WDWHOHVV��6HVVLRQ�&RQWDLQHU�0DQDJHG�ILHOGV��(QWLW\�&ODVV�QDPHV��VHFXULW\��WUDQVDFWLRQV��ERWK�
30
© Copyright IBM Corporation 1999
Deploy Beans to EJB Server
Write EJB Client(s)
Test Beans and EJB Client(s)
Write Remote Interface
Write Home
Interface
Write Bean Class
EJB Development Process
31
© Copyright IBM Corporation 1999
Counter Example
:HOO�VKRZ�WKH�SURFHVV�RI�EXLOGLQJ�D�VLPSOH�6WDWHIXO�6HVVLRQ�EHDQRQH�LQVWDQFH�YDULDEOHLQFUHPHQW����UHVHW���DQG�YDOXH���PHWKRGV
,W�FRXOG�DFW�DV�D�VLPSOH��KLW�FRXQWHU��IRU�D�ZHE�SDJH
32
© Copyright IBM Corporation 1999
Home Interfaces
$OO�(-%V�KDYH�+RPH�,QWHUIDFHV
([WHQG�MDYD[�HME�(-%+RPH
'HILQHV�FUHDWH���PHWKRGV6SHFLI\�D�FUHDWH���PHWKRG�IRU�HDFK�ZD\�RI�EXLOGLQJ�D�EHDQ��SDUDPHWHU�VHW�
CounterHome
create()create(int )
javax.ejb.EJBHome<<interface>>
<<interface>>
java.rmi.Remote<<interface>>
33
Only Stateful session beans have more than one (default) create() method. create() methods vary only in the number of parameters they take. As we will see, each new create() method corresponds one-to-one with an ejbCreate() method in the Bean.
The supertype (EJBHome) defines three methods
public void remove (Handle handle) throws java.rmi.RemoteExcpetion, javax.ejb.RemoveException;
public void remove(Object pKey) throws java.rmi.RemoteException, javax.ejb.RemoveException;
(You generally don’t remove() Session beans -- removing by a primary Key will only work for Entity beans)
public EJBMetaData getEJBMetaData() throws java.rmi.RemoteException;
The EJBMetaData allows you to get to information like the EJBHome, the Home interrface class, the Remote interface class, and hte primary key class. It also allows you to ask if a bean is a session or entity bean.
© Copyright IBM Corporation 1999
Counter Home Example
package com.ibm.ejb.examples.simple;
/*** This is a Home Interface **/public interface CounterHome extends javax.ejb.EJBHome { public Counter create() throws javax.ejb.CreateException,
java.rmi.RemoteException; public Counter create(int arg1) throws javax.ejb.CreateException,
java.rmi.RemoteException;}
34
© Copyright IBM Corporation 1999
Remote Interfaces
5HPRWH�,QWHUIDFHV�GHILQH�WKH�EXVLQHVV�ORJLF�RI�D�%HDQ
([WHQG�MDYD[�HME�(-%2EMHFW
'HILQH�UHPRWH�EXVLQHVV�ORJLF�PHWKRGV3ULYDWH�ORFDO�PHWKRGV�QHHG�QRW�EH�LQ�WKH�UHPRWH�LQWHUIDFH
javax.ejb.EJBObject<<interface>>
java.rmi.Remote<<interface>>
Counter
void increment()void reset()
<<interface>>
int getValue()
35
© Copyright IBM Corporation 1999
Counter Remote Interface
package com.ibm.ejb.examples.simple;
/*** This is an Enterprise Java Bean Remote Interface*/public interface Counter extends javax.ejb.EJBObject {
int getValue() throws java.rmi.RemoteException;
void increment() throws java.rmi.RemoteException;
void reset() throws java.rmi.RemoteException;}
36
© Copyright IBM Corporation 1999
Implementing a Session Bean
,PSOHPHQWLQJ�D�VHVVLRQ�LQYROYHV�,PSOHPHQWLQJ�WKH�6HVVLRQ%HDQ�,QWHUIDFH,PSOHPHQWLQJ�\RXU�EXVLQHVV�ORJLF,PSOHPHQWLQJ�OLIHF\FOH�PHWKRGV
2QFH�\RX�DUH�GRQH�\RX�FDQ�GHSOR\�DQG�JR�
37
The lifecycle methods are methods defined in the SessionBean interface. You need to implement all of them, but WHAT you do in each method depends on the bean. The next two slides discuss these methods in more detail.
© Copyright IBM Corporation 1999
Creating Stateful Beans
<RX�FUHDWH�D�VWDWHIXO�EHDQ�XVLQJ�FUHDWH���PHWKRGV�GHILQHG�LQ�WKH�+RPH�,QWHUIDFH8VH�DQ\�QXPEHU�RI�SDUDPHWHUV
(DFK�FUHDWH���PHWKRG�LQ�WKH�+RPH�LQWHUIDFH�FRUUHVSRQGV�WR�DQ�HME&UHDWH���LQ�WKH�%HDQ�,PSOHPHQWDWLRQ��H�J�SXEOLF�&RXQWHU�FUHDWH�LQW�DUJ��SXEOLF�YRLG�HME&UHDWH�LQW�DUJ��
38
© Copyright IBM Corporation 1999
Passivation/Activation
$Q�(-%�VHUYHU�KDV�WKH�ULJKW�WR�PDQDJH�LWV�ZRUNLQJ�VHW�3DVVLYDWLRQ����VDYHV�VWDWH�RI�D�EHDQ�WR�SHUVLVWHQW�VWRUDJH��WKHQ�VZDSV�LW�RXW��$FWLYDWLRQ����UHVWRUHV�VWDWH�RI�D�EHDQ�IURP�SHUVLVWHQW�VWRUDJH��WKHQ�VZDSV�LW�LQ��
2QO\�QHFHVVDU\�IRU�VWDWHIXO�VHVVLRQ�EHDQV
39
The problem is with Stateful beans they take up room since you must have MANY instances running around (one per client). You don’t want them all in memory at once!
Entity Beans can also be passivated and activated.
© Copyright IBM Corporation 1999
SessionBean lifecycle methods
HME$FWLYDWH�����FDOOHG�ZKHQ�EHDQ�LV�DFWLYDWHG���ORDG��LQVWDQFH�YDULDEOHV�
HME3DVVLYDWH�����FDOOHG��EHIRUH�EHDQ�LV�SDVVLYDWHG���VDYH��LQVWDQFH�YDULDEOHV
HME5HPRYH�����FDOOHG�EHIRUH�EHDQ�LV�GHVWUR\HG���FOHDQ�XS��
VHW6HVVLRQ&RQWH[W�6HVVLRQ&RQWH[W�FW[����FDOOHG�E\�FRQWDLQHU�WR�JLYH�EHDQ�D�FRQWH[W
40
ejbActivate() and ejbPassivate() are methods that must be implemented, but the do not need to do anything. When a bean isselected for passivation, the ejbPassivate() method will be called in the bean before the bean is serialized to a file.
Likewise, after a bean is loaded from a serialized file, the ejbActivate() method is called in that bean.
These methods are generally used to setup and tear-down instance variables that cannot be serialized -- either variables that contain transient information (like caches) or non-serializable objects (like AWT objects, or TCP connections).
© Copyright IBM Corporation 1999
Contexts
EJBContext
getRollbackOnly () : booleansetRollbackOnly () : voidgetEJBHome () : javax.ejb.EJBHomegetEnvironment () : java.util.PropertiesisCallerInRole ( : java.security.Identity) : booleangetCallerIdentity () : java.security.IdentitygetUserTransaction () : javax.jts.UserTransaction
<<interface>>
EntityContext
getEJBObject () : javax.ejb.EJBObjectgetPrimaryKey () : Object
<<interface>>SessionContext
getEJBObject () : javax.ejb.EJBObject<<interface>>
8WLOLW\�REMHFWV
SDVVHG�LQ�WKH�VHW;;;&RQWH[W���PHWKRG
41
Session beans implement setSessionContext(). Entity beans implement setEntityContext().Here is the definition of EJBContext:
public interface javax.ejb.EJBContext{public abstract Identity getCallerIdentity();public abstract EJBHome getEJBHome();public abstract Properties getEnvironment();public abstract boolean getRollbackOnly();public abstract UserTransaction getUserTransaction();public abstract boolean isCallerInRole(Identity role);public abstract void setRollbackOnly();}
getCallerIdentity is used in EJB Security to obtain the identity of the caller. Likewise isCallerInRole() allows you to ask if the caller is acting in a role. These features is not used in WebSphere 2.0. getEJBHome obtains a rference to the EJB Home for this bean. We will look at using getUserTransaction(), setRollbackOnly() and getRollbackOnly() in the section on transactions.
© Copyright IBM Corporation 1999
SessionContext
H[WHQGV�MDYD[�HME�(-%&RQWH[W
SXEOLF�DEVWUDFW�(-%2EMHFW�JHW(-%2EMHFW��2EWDLQ�D�UHIHUHQFH�WR�WKH�(-%�REMHFW�WKDW�LV�FXUUHQWO\�DVVRFLDWHG�ZLWK�WKH�LQVWDQFH�$Q�LQVWDQFH�FDQ�XVH�WKLV�PHWKRG��IRU�H[DPSOH��ZKHQ�LW�ZDQWV�WR�SDVV�D�UHIHUHQFH�WR�LWVHOI�LQ�D�PHWKRG�DUJXPHQW�RU�UHVXOW�
42
The reason you want to use getEJBObject() to send a message to yourself and not this is that the EJBObject is the one that handles issues of transactions, multithreading and persistence. You bypass all of that if you use this.
© Copyright IBM Corporation 1999
Counter Example Implementation
package com.ibm.ejb.examples.simple;/** Lots of import statements left out for space **/
public class CounterBean implements javax.ejb.SessionBean { private javax.ejb.SessionContext mySessionCtx = null;public int counterValue =0;
public void ejbActivate() throws java.rmi.RemoteException {}public void ejbCreate() {}public void ejbCreate(int initialValue) {
counterValue = initialValue;}public void ejbPassivate() throws java.rmi.RemoteException {}public void ejbRemove() throws java.rmi.RemoteException {}public int getCounterValue() { return counterValue; }public void increment() {counterValue++;}public void reset() {counterValue = 0;}public void setSessionContext(javax.ejb.SessionContext ctx) throws java.rmi.RemoteException {
mySessionCtx = ctx;}}
43
There’s a lot here to talk through. The bits in RED are the lifecycle methods. These are automatically created by VA-J. If you’re not using VA-J, then you’ll have to code them yourself. Note that most of the lifecycle methods do nothing.
The parts in blue are the business logic methods. You will always code these yourself.
Note that every method can throwjava.rmi.RemoteException. You can also decide to throw an application-defined exception in your methods. Your business method may also throw the java.rmi.RemoteException to indicate a system-level failure.
© Copyright IBM Corporation 1999
Deploying EJBs
(-%V�DUH�GHSOR\HG�DV�-DU�ILOHV
-DU�ILOHV�FRQWDLQ�0DQLIHVW�ILOH�XVHG�WR�OLVW�(-%V�+RPH��5HPRWH�LQWHUIDFHV�DQG�EHDQ�LPSOHPHQWDWLRQ�FODVVHV'HSOR\PHQW�'HVFULSWRUV
8VHG�E\�WKH�&RQWDLQHU�WR�JHQHUDWH�DGGLWLRQDO�FODVVHV�IRU�GLVWULEXWLRQ�DQG�SHUVLVWHQFH
44
A Jar file is a standard Java deployment format. It’s really just a ZIP file.
There are tools to take your .class files and create the appropriate JAR files that we’ll see next.
© Copyright IBM Corporation 1999
Generated Classes Example
_CounterHomeStub
_CounterStub
CounterHelper
CounterHomeHelper_impl
_CounterHomeSkeleton
valueCounterHomeCounterHomeHolder
_impl
_CounterSkeleton
value CounterCounterHolder
EJSRemoteCounter
EJSCounterHome
CounterBean
ebRef
getBean()
45
The classes can be grouped together as follows:
_XXXStub and _XXXSkeleton classes are RMI Stubs and Skeletons
EJSXXX classes are the Container-generated objects that handle the lifecycle and frameworkparts of the system.
XXXHelpers are CORBA objects used in the narrow() operation with JNDI
XXXHolders are CORBA objects that are used in marshalling objects in a CORBA "Any"
© Copyright IBM Corporation 1999
EJB Clients
9HU\�PXFK�OLNH�50,�FOLHQWV
8VH�-1',�WR�ORFDWH�+RPHV
8VH�+RPHV�WR�REWDLQ�(-%�VWXEV
46
© Copyright IBM Corporation 1999
JNDI
-1',��-DYD�1DPLQJ�DQG�'LUHFWRU\�,QWHUIDFH�0DWFKHV�QDPHV�ZLWK�2EMHFWV
(DFK�YHQGRU�SURYLGHV�DQ�LPSOHPHQWDWLRQ�RI�FHUWDLQ�-'1,�FODVVHV$�6HUYLFH�3URYLGHU�,QWHUIDFH�RU�63,
:HE6SKHUH�XVHV�&25%$�1DPLQJ
JNDI API
IBM Cos Naming
SPI
Other SPIs...
Client Program
47
In our case the names will be matched to a Home. We will, of course, get back a Proxy to that Home.
© Copyright IBM Corporation 1999
JNDI Details
7KH�-1',�$3,�LV�IRXQG�LQ�WKH�MDYD[�QDPLQJ�SDFNDJH
$�&RQWH[W�LV�DQ�REMHFW�WKDW�ELQGV�WRJHWKHU�QDPHV�ZLWK�2EMHFWV
&RQWH[WV�FDQ�EH�QHVWHG��GLUHFWRU\�VWUXFWXUH�
7KH�7RS�OHYHO�&RQWH[W�LV�FDOOHG�WKH�,QLWLDO&RQWH[W
48
There is another package javax.naming.directory that is used for nested contexts. We won’t use it in this course.
You will want to import the javax.naming package into all of your clients.
InitialContext is a concrete class that implements the Context interface. In EJB’s today you most often will only use an InitialContext --you will probably not take advantage of the directory features of Contexts.
© Copyright IBM Corporation 1999
Binding
7KH�&RQWH[W�LQWHUIDFH�GHILQHV�WKH�IROORZLQJ�PHVVDJHVYRLG�ELQG�1DPH�QDPH��2EMHFW�REM�2EMHFW�ORRNXS�1DPH�QDPH�
$�FRQWDLQHU�DXWRPDWLFDOO\�ELQGV�WKH�+RPHV�LW�FRQWDLQV�ZKHQ�LW�VWDUWV�XS
$�FOLHQW�VLPSO\�ORRNV�XS�WKH�ULJKW�+RPH
49
The whole interface is kind of similar to a Hashtable -- it puts together key-value pairs.
Which Homes a container binds to is determined as part of the EJB deployment process, which we will look at later.
© Copyright IBM Corporation 1999
InitialContext in EJS
2EWDLQ�DQ�,QLWLDO&RQWH[W�E\�XVLQJ�WKH�,QLWLDO&RQWH[W�+DVKWDEOH��FRQVWUXFWRU
7KH�+DVKWDEOH�FRQWDLQV�WKH�HQYLURQPHQW�IRU�WKH�,QLWLDO&RQWH[W,1,7,$/B&217(;7B)$&725<3529,'(5B85/
7KH�FRQVWUXFWRU�FDQ�WKURZ�D�1DPLQJ([FHSWLRQ
50
There 12 or so possible environment variables that can be set in the Hashtable. You can view them in the Javadocs. EJS only uses the two listed above.
The INITIAL_CONEXT_FACTORY environment variable tells JNDI which SPI to use for this particular Context. The PROVIDER_URL tells the Cos Naming SPI where to locate the Persistent naming service (PNS). The value of PROVIDER_URL should be set to the URL of the naming service (localhost would just be "iiop:///"). The value of INITIAL_CONTEXT_FACTORY should always be set to "com.ibm.jndi.CosNaming.CNInitalContextFactory" (the EJS implementation class that builds Contexts in this version of WebSphere).
© Copyright IBM Corporation 1999
Getting an InitialContext
javax.naming.InitialContext initContext = null;java.util.Hashtable properties = new java.util.Hashtable(2);properties.put(javax.naming.Context.PROVIDER_URL, "iiop:///");
// local nameserver URLproperties.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.jndi.CosNaming.CNInitialContextFactory"); // IBM name services factory
try {initContext = new javax.naming.InitialContext(properties);
} catch (NamingException e) {System.out.println("Exception: " + e);
}
51
This is the start of a simple example of a Counter.
The Remote interface declares an increment() method, a reset() mtehod and a getValue() method. The Home interface is very simple -- it just has a default create() method, and a create(int) method that allows you to create a counter preset to a particular value.
The value of the PROVIDER_URL property is set to an iiop URL of the naming service. / means go to the default port on localhost. Otherwise use the iiop://hostname:port style.
Note: In a real system you would never want to hard code these values as this example shows. Instead you would use a properties file to look these values up at runtime.
© Copyright IBM Corporation 1999
Using Home Interfaces
(YHU\�NLQG�RI�(-%�KDV�D�+RPH�,QWHUIDFH
<RX�REWDLQ�D�+RPH�UHIHUHQFH�IURP�D�-1',�,QLWLDO&RQWH[W
,Q�:HE6SKHUH�\RX�XVH�D�&25%$�+HOSHU�REMHFW�WR��QDUURZ����LW�WR�WKH�FRUUHFW�W\SH
7KH�(-%�6SHF�GHILQHV�D�VLPLODU�PHWKRG�LQ�MDYD�UPL�3RUWDEOH5HPRWH2EMHFW
52
Remember that the Home interface defines the create() and find() methods that allow you to obtain references (Proxies) to EJB’s that live on an EJB Server.
You define the JNDI name that a Home is known in the deployment descriptor.
In EJS the Helper object is automatically generated by the deployment tool when you deploy your EJB. A "narrow()" operation is kind of like a cast -- it takes a generic org.omg.CORBA.Object and "retrieves" its type information so that it can be used.
By the way -- this implementation (for EJS) is different from the default implementattion mentionede in the EJB spec -- in the spec the class java.rmi.PortableObject.narrow() is used instead of the helper narrow() method we use with EJS.
© Copyright IBM Corporation 1999
Obtaining a Home Reference
CounterHome home = null;try {
// The InitialContext was retrieved earlierObject obj = initContext.lookup("Counter"); // "Counter" is the JNDI name of the Homeif (obj instanceof org.omg.CORBA.Object) {
home = CounterHomeHelper.narrow((org.omg.CORBA.Object) obj);} else {
System.out.println("Lookup returned unexpected object type.");}
} catch (javax.naming.NamingException e) {System.out.println("Error retrieving the home interface:" + e);
}
53
Remember the proxy pattern. CounterHome is the interface that things implement -- this is why the home variable is typed to theCounterHome Interface. The actual object you get back is a proxy to a Home object on the server.
© Copyright IBM Corporation 1999
Obtaining Beans
2QFH�\RX�KDYH�D�+RPH�\RX�FDQ�XVH�FUHDWH���RU�ILQG���PHWKRGV�WR�JHW�%HDQ�VWXEV2QFH�\RX�KDYH�D�VWXE�\RX�FDQ�VWDUW�VHQGLQJ�LW�EXVLQHVV�PHWKRGV
try {Counter counter = counterHome.create();counter.reset();counter.increment();counter.increment();int value = counter.getCounterValue();System.out.println("the value is " + value);
} catch (Exception e) {System.out.println("Exception creating new SimpleCounter:" + e);
}
54
find() methods are only used for Entity beans.
© Copyright IBM Corporation 1999
Entity Basics
$Q�(QWLW\�%HDQ�UHSUHVHQWV�D�SHUVLVWHQW�EXVLQHVV�REMHFW
$OO�(QWLW\�%HDQV�OLYH�LQ�D�FRQWDLQHU
0XOWLSOH�FOLHQWV�FDQ�FRQFXUUHQWO\�DFFHVV�DQ�(QWLW\�%HDQ
(QWLW\�%HDQV�KDYH�D�OLIHWLPH�EH\RQG�WKH�FOLHQW�DQG�VHUYHU�SURFHVVHV
55
The container transparently manages concurrency, transactions, persistence and other services for the beans that live in it. Entity Beans are persistent -- they are stored in some sort of database. They will survive server crashes, etc.
© Copyright IBM Corporation 1999
Home Interfaces for Entities
7KH�+RPH�,QWHUIDFH�IRU�DQ�(QWLW\�%HDQ�DOORZV�D�FOLHQW�WR�&UHDWH�QHZ�(-%�REMHFWV��ZLWK�FUHDWH���PHWKRGV�/RRN�XS�H[LVWLQJ�(-%�2EMHFWV��ZLWK�ILQGHU�PHWKRGV�5HPRYH�(-%�2EMHFWV
56
Each create() method in a Home Interface corresponds to a ejbCreate() method with the same parameter set in the Bean class. You can write create() methods that fill in all, some, or none of the fields in an Entity bean.
© Copyright IBM Corporation 1999
Finder Methods
$�+RPH�,QWHUIDFH�GHILQHV�RQH�RU�PRUH�ILQGHU�PHWKRGV�WR�ORRN�XS�(-%�2EMHFWV
$�ILQGHU�PHWKRG�DOZD\V�VWDUWV�ZLWK�WKH�ZRUG��ILQG�FDQ�UHWXUQ�WKH�5HPRWH�LQWHUIDFH�RU�D�&ROOHFWLRQ���(QXPHUDWLRQ�
(YHU\�+RPH�,QWHUIDFH�IRU�(QWLW\�EHDQV�PXVW�GHILQH��ILQG%\3ULPDU\.H\���
57
he "Collection" that is defined in the Spec must ALWAYS be an Enumeration, e.g., a finder method can only return the Remote Interface type or an Enumeration.
© Copyright IBM Corporation 1999
Sample Home Interface
public interface EmployeeHome extends EJBHome {
public Employee create (EmployeeKey primaryKey) throws CreateException, RemoteException;
Employee create(EmployeeKey key, String name, String dept, String phone) throws CreateException, RemoteException;
Enumeration findAll() throws RemoteException, FinderException;
public Employee findByPhoneNumber(String phoneNumber) throws RemoteException, FinderException;
public Employee findByPrimaryKey (EmployeeKey primaryKey) throws RemoteException, FinderException;
}
58
This Home Interface demonstrates most of the concepts discussed on the previous slides:
(1) It has two create() methods -- one that takes the PrimaryKey class (EmployeeKey) and another that takes the PrimaryKey plus all of the fields of the object.(2) It has three finder methods;
(a) The default finder method "findByPrimaryKey()"(b) A finderMethod that takes a singleargument "findByPhoneNumber()" and returns an EJBobject that corresponds to the Remote interface(c) A finderMethod that returns an Enumeration of objects that correspond to theRremote interface.
The key thing to remember is that this only specifies WHAT needs to be done -- not HOW. The HOW is defined by the container or thebean.
© Copyright IBM Corporation 1999
The EJS Persistence model
5HPHPEHU�WKDW�(QWLW\�%HDQV�FRPH�LQ�WZR�IRUPV%HDQ�0DQDJHG�3HUVLVWHQFH��%03�&RQWDLQHU�0DQDJHG�3HUVLVWHQFH��&03�
(YHU\�YHQGRU�LPSOHPHQWV�(QWLW\�EHDQV�VOLJKWO\�GLIIHUHQWO\
,Q�WKH�IROORZLQJ�VHFWLRQ�ZH�ZLOO�JR�EH\RQG�WKH�(-%�VSHF�WR�H[DPLQH�WKH�:HE6SKHUH�&03�PRGHO
59
For instance, we will cover the WebSphere-specific way of implementing finder methods. While the spec says that finder methods can exist, it gives no hints as to how they should be implemented, leaving each vendor to do it themselves.
© Copyright IBM Corporation 1999
Specifying Fields
7KH�&RQWDLQHU�PXVW�EH�LQIRUPHG�RI�ZKDW�ILHOGV�LQ�HDFK�EHDQ�VKRXOG�EH�SHUVLVWHG7KLV�LV�GRQH�E\�VHWWLQJ�WKH�FRQWDLQHU0DQDJHG)LHOGV�OLQH�LQ�WKH�GHSOR\PHQW�GHVFULSWRU$OO�FRQWDLQHU�PDQDJHG�ILHOGV�PXVW�EH�RI�D�6HULDOL]DEOH�W\SH
7KLV�LQIRUPDWLRQ�LV�XVHG�WR�JHQHUDWH�WKH�(-6�GHSOR\PHQW�FODVVHV�IRU�SHUVLVWHQFH
60
This part of the Deployment descriptor is generated and handled by VisualAge for Java if you are using that tool.
© Copyright IBM Corporation 1999
Basic EJS Persistence
7KH�PRVW�EDVLF�PDSSLQJ�IRU�(QWLW\�%HDQV�PDSV�HDFK�SHUVLVWHQW�LQVWDQFH�YDULDEOH�WR�D�FROXPQ�LQ�D�WDEOH(DFK�EHDQ�PDSV�WR�D�VLQJOH�'%��WDEOH7KH�WDEOH�LV�FUHDWHG�WKURXJK�64/�EXLOW�LQ�WKH�SHUVLVWHQFH�FODVVHV6LPSOH��IL[HG�PDSSLQJ�RI�ILHOG�W\SH�WR�64/�W\SH
61
The table is automatically created by WebSphere during deployment. Note that DB2 has a table-length restriction that all table names must be less than 18 characters. The table is automatically generated to have the following form:
EJB.<JNDINAME>BeanTbl
Where JNDINAME is the JNDI Name of the bean (also the remote interface name). If your EJBRemoteInterface name is more than 11 characters, this generation will fail and you will have to rename your Remote Interface.
© Copyright IBM Corporation 1999
Standard Type Mapping
CMP field Java Type SQL TypeString VARCHARShort / short SMALLINTInteger / int INTEGERFloat / float FLOATDouble / double DOUBLEByte / byte SMALLINTLong / long VARCHAR(22)Character / char CHAR(1)Boolean / boolean SMALLINTJava.math.BegDecimal NUMERICJava.sql.Date DATEJava.sql.Time TIMEJava.sql.Timestamp TIMESTAMPOther (serialize) BLOB
62
One thing to point out in this diagram is that types other than the primitive types are mapped to 1 Megabyte BLOBs. This is not the kind of mapping that you want to use.
© Copyright IBM Corporation 1999
FinderHelpers
7KH�+RPH�FODVV�GRHV�QRW�NQRZ�E\�LWVHOI�KRZ�WR�LPSOHPHQW�ILQGHU�PHWKRGV<RX�PXVW�SURYLGH�WKHVH�DV�FRQVWDQWV��VWDWLF�ILQDO�6WULQJ��LQ�WKH�)LQGHU+HOSHU�FODVV$GG�RQH�ILHOG�SHU�ILQGHU�PHWKRG��H[FHSW�IRU�WKH�ILQG%\3ULPDU\.H\�PHWKRG��7KHUH�DUH�DV�PDQ\�TXHU\�SDUDPHWHUV�DV�WKHUH�DUH�SDUDPHWHUV�WR�WKH�PHWKRG8VH�WKH�SDWWHUQ�;;;4XHU\6WULQJ�ZKHUH�;;;�LV�WKH�IXOO�QDPH�RI�WKH�ILQGHU�PHWKRG�
63
© Copyright IBM Corporation 1999
FinderHelper Example
/** * For each finder method defined in the home interface other than the * findByPrimaryKey method, a query String must be defined here.**/
public interface EmployeeBeanFinderHelper {
public static final String findAllQueryString = "select * from EJB.EmplyeeBeanTbl";
public static final String findByPhoneNumberQueryString = "select * from EJB.EmplyeeBeanTbl where phoneNumber = ?";
}
64
To find the table name and column names for the class, refer to the class EJSJDBCPersisterXXXBean class in the method in the text of the method getCreateTableSQLString()
Note that this is the potential here for hard-to-find errors. If your table definition changes (due to the addition of new fields) your finder helper classes will NOT automatically be updated. This will result in your finder methods failing with null pointer exceptions.
© Copyright IBM Corporation 1999
PrimaryKeys
(YHU\�(QWLW\�%HDQ�DOVR�QHHGV�D�SULPDU\�NH\�FODVV
7KLV�FODVV�DOORZV�\RX�WR�FRPSDUH�REMHFWV7KH�(-%2EMHFW�PHWKRG�LV,GHQWLFDO���FRPSDUHV�WZR�SULPDU\�NH\V<RXU�FODVV�PXVW�LPSOHPHQW�6HULDOL]DEOH�DQG�LPSOHPHQW�WKH�PHWKRGV�HTXDOV�2EMHFW��DQG�KDVK&RGH��
65
© Copyright IBM Corporation 1999
Advanced WebSphere Container Support
9LVXDO$JH�IRU�-DYD�SURYLGHV��FRPSOH[�PDSSLQJV�IURP�:HE6SKHUH�&03�(-%V�WR�5HODWLRQDO�GDWDEDVHV0XOWLSOH�WDEOHV�WR�(-%V7\SH�FRQYHUVLRQV
7KH�QH[W�YHUVLRQ�ZLOO�KDQGOH�LQKHULWDQFH�DQG�UHODWLRQVKLSV&XUUHQWO\�GRQH�E\�KDQG
66
© Copyright IBM Corporation 1999
Bean Managed Entity Beans
8VHG�IRU�LGHQWLILDEOH�REMHFWV�WKDW�0XVW�PDS�WR�EDFN�HQG�VRXUFHV�QRW�VXSSRUWHG�E\�D�FRQWDLQHU
&UHDWH�\RXU�RZQ�%HDQ�FODVV�WKDW,PSOHPHQWV�MDYD[�HME�(QWLW\%HDQ�LQWHUIDFH,V�PDUNHG�DV�%($1B0$1$*('��LQ�GHSOR\PHQW�GHVFULSWRU,PSOHPHQWV�PHWKRGV�WR�ORDG�DQG�VWRUH�WKH�EHDQ�GDWD
67
© Copyright IBM Corporation 1999
EntityBean InterfaceHME$FWLYDWH������FDOOHG�RQ�DFWLYDWLRQ�
HME3DVVLYDWH������FDOOHG�RQ�SDVVLYDWLRQ�
HME/RDG������WHOOV�EHDQ�WR�ORDG�VWDWH�IURP�GDWDEDVH�
HME6WRUH������WHOOV�EHDQ�WR�VWRUH�VWDWH�LQ�GDWDEDVH�
HME5HPRYH������FDOOHG�ZKHQ�FOLHQW�FDOOV�UHPRYH���
68
© Copyright IBM Corporation 1999
Passivation Sequence
Client EJBObject BMP Bean Instance
DatabaseContainer
business methodbusiness method
ejbStore() write state to db
ejbPassivate()
69
Writing the state to the database is done through an UPDATE. This is the sequence that is called if the business method is a transaction, and the object has been already activated.
© Copyright IBM Corporation 1999
Activation Sequence
DatabaseClient EJBObject BMPBean Instance
business method ejbActivate()
ejbLoad()read state from db
70
During the EJB Load you will need to SELECT from a database. This is the sequence that is called if the object has not yet been activated.
© Copyright IBM Corporation 1999
Commit Sequence
Client Container BMPClass Instance
Trasaction Service
Database
UserTransaction.commit()
beforeCompletion()
ejbStore()write state to db
afterCompletion()
ejbPassivate()
71
© Copyright IBM Corporation 1999
Example ejbLoad()public void ejbLoad() throws java.rmi.RemoteException {
if (connection == null)throw new java.rmi.RemoteException("No database connection");
// Use the connection to store the new bean into the database:ResultSet rs = null;try {
// Execute the SQL:PreparedStatement preparedStatement = connection.prepareStatement(SELECT_SQL);preparedStatement.setInt(1, ((EmployeeKey) entityContext.getPrimaryKey()).primaryKey);rs = preparedStatement.executeQuery();// Pull the data out of the result set:if (rs.next()) {
customerNumber = rs.getInt(1);firstName = rs.getString(2).trim();lastName = rs.getString(3).trim();// address, city, state, zip are the same
} else {throw new RemoteException("Unable to load data");
}} catch (SQLException e) {
throw new RemoteException(e.getMessage(), e);} finally {
try { if (rs != null)rs.close();
} catch (SQLException e) {}}
}
72
© Copyright IBM Corporation 1999
Example ejbStore()
public void ejbStore() throws java.rmi.RemoteException {
if (connection == null)throw new java.rmi.RemoteException("No database connection");
try {PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_SQL);preparedStatement.setString(1, firstName);preparedStatement.setString(2, lastName);preparedStatement.setInt(8, customerNumber);preparedStatement.setString(3, address);preparedStatement.setString(4, city);preparedStatement.setString(5, state);preparedStatement.setString(6, zip);preparedStatement.setString(7, age);preparedStatement.executeUpdate();
} catch (SQLException e) {throw new RemoteException("SQL Problem " + e);
}}
73
© Copyright IBM Corporation 1999
EJB Transaction Model
(-%�SURYLGHV�WUDQVDFWLRQ�VXSSRUW�DXWRPDWLFDOO\�PDQDJHV���FRRUGLQDWHV�ORZ�OHYHO�WUDQVDFWLRQ�GHWDLOV�IRU�SURJUDPPHUV
'HFODUDWLYH�%HDQ�7UDQVDFWLRQ�DWWULEXWHV�SURYLGH�UXQWLPH�WUDQVDFWLRQ�LQIRUPDWLRQ�WR�&RQWDLQHUV
%HDQ�GHYHORSHUV�DQG�%HDQ�FOLHQWV�PD\�LQWHUYHQH�WR�H[SOLFLWO\�PDQDJH�WUDQVDFWLRQV
74
© Copyright IBM Corporation 1999
Transaction Definition
$�XQLW�RI�ZRUN�WKDW�LV�$WRPLF��,I�,QWHUUXSWHG�E\�IDLOXUH�DOO�HIIHFWV�DUH�XQGRQH��UROOHG�EDFN�&RQVLVWHQW��(IIHFWV�RI�D�WUDQVDFWLRQ�SUHVHUYH�LQWHJULW\�RI�PRGLILHG�UHVRXUFHV,VRODWHG��,QWHUPHGLDWH�VWDWHV�DUH�WUDQVSDUHQW��DQG�LW�WKHUHIRUH�DSSHDUV�WR�H[HFXWH�VHULDOO\�'XUDEOH��(IIHFWV�RI�D�FRPSOHWHG�WUDQVDFWLRQ�DUH�SHUPDQHQW�DQG�QHYHU�ORVW
75
Related work means things like database updates.
© Copyright IBM Corporation 1999
Sun Transaction Standards
-DYD�7UDQVDFWLRQ�$3,��-7$�6XQ·V�VSHFLILFDWLRQ�RI�KLJK�OHYHO�-DYD�LQWHUIDFHV�EHWZHHQ�D�WUDQVDFWLRQ�PDQDJHU�DQG�WKH�SDUWLHV�LQYROYHG�LQ�D�GLVWULEXWHG�WUDQVDFWLRQ�V\VWHP�
-DYD�7UDQVDFWLRQ�6HUYLFH��-76�6XQ·V�LPSOHPHQWDWLRQ�RI�D�7UDQVDFWLRQ�0DQDJHU�ZKLFK�VXSSRUWV�-7$,PSOHPHQWV�WKH�-DYD�PDSSLQJ�RI�WKH�20*�2EMHFW�7UDQVDFWLRQ�6HUYLFHV��276�����
76
The parties involved in a transaction system are:
ApplicationResource Managerand Application Server
The EJB spec requires a partial implementation of JTA. This hides most system complexity from the programmer.
© Copyright IBM Corporation 1999
Transaction Standards
;�23(1�;$�3URWRFRO
�20*�&25%$�276
-7$��-76
(-%�6SHF
3URJUDP
77
This pyramid makes several assumptions:
(1) That the EJB Vendor provides a Transaction Manager that implements the JTA and JTS (this is true of WebSphere).(2) That other vendors (or the same vendor) provide XA compliant resource managers. The 3.0 version of IBM WebSphere will contain XA compliant database drivers for Oracle, Sybase and DB2 that will also comply with the JDBC 2.0 standard. The JDBC 2.0 Standard allows for XA compliance, but does not mandate it.
© Copyright IBM Corporation 1999
Transaction Demarcation
7UDQVDFWLRQDO�EHKDYLRU�LV�GHILQHG�LQ�WKH�GHSOR\PHQW�GHVFULSWRU
&DQ�EH�VHW�IRU�WKH�FODVV�DV�D�ZKROH��RU�IRU�HDFK�PHWKRG
$OO�(-%V�DUH�WUDQVDFWLRQDO��EXW�-DYD�FODVVHV�XVHG�E\�WKHP�DUH�QRW5HTXLUHV�FDUHIXO��DWWHQWLRQ�WR�GHVLJQ
78
© Copyright IBM Corporation 1999
Transaction DemarcationClients TXN TXN Associated
with EJB Method
TX_NOT_SUPPORTED - -T1 -
TX_REQUIRED - T2
T1 T1
TX_SUPPORTS - -
T1 T1
TX_REQUIRES_NEW - T2
T1 T2
TX_MANDATORY - ERROR
T1 T1
79
TX_REQUIRED is default in VAJ.
© Copyright IBM Corporation 1999
Bean Managed Transactions
7;B%($1B0$1$*('�WUDQVDFWLRQ�DWWULEXWH0HDQV�(-%�0HWKRGV�GHPDUFDWH�WKHLU�RZQ�WUDQVDFWLRQV
%HDQ�XVHV�WKH�MDYD[�MWV�8VHU7UDQVDFWLRQ�,QWHUIDFHEHJLQ����FRPPLW����JHW6WDWXV����UROOEDFN���
$YRLG�RSHQ�WUDQVDFWLRQV�DW�PHWKRG�FRPSOHWLRQ
80
The bean can obtain the user transaction by going to the Entity or Session Context.
© Copyright IBM Corporation 1999
Java Message Service
$�VWDQGDUG�-DYD�$3,�IRU�0HVVDJH�RULHQWHG�0LGGOHZDUH3XEOLVK�6XEVFULEH�DQG�3RLQW�WR�3RLQW6WDQGDUG�0HVVDJH�W\SHV
'RHV�IRU�020�ZKDW�-'%&�GLG�IRU�'DWDEDVHV
,%0�KDV�DQQRXQFHG�XSFRPLQJ�VXSSRUW�IRU�04�6HULHV7KH�EDVH�IURP�ZKLFK�RWKHU�WKLQJV�ZLOO�JURZ2WKHU�YHQGRUV�DUH�DOVR�PDNLQJ�DQQRXQFHPHQWV
81
© Copyright IBM Corporation 1999
Distributed EJB Transactions
Application/EJB Server
Transaction Manager
Resource ManagerResource
Manager
SQL, etc.
OTS TX
XA
JDBC 2.0JMS
82
© Copyright IBM Corporation 1999
EJB Program Architectures
&OLHQWV�VKRXOG�QRW�EH�DZDUH�RI�WKH�FRPSOH[LW\�RI�\RXU�HQWLW\�PRGHO
$�VHUYHU�REMHFW�PDQDJHV�UHODWLRQVKLSV7KH�UROH�RI�D�VWDWHIXO�VHVVLRQ�EHDQ
7KH�VHVVLRQ�EHDQ�DFWV�D�)DFDGH�RQWR�RWKHU�GDWD�VRXUFHV&03�EHDQV%03�EHDQV�XVLQJ�DQRWKHU�SHUVLVWHQFH�IUDPHZRUN�
83
Facade is a design pattern from Gamma, et. al. A Facade is an object that hides the complex relationships between other objects.
© Copyright IBM Corporation 1999
Session Bean Facades
Clients see only the Facade -- they don’t see the underlying
relationships
Session BeanSubsystem
84
In cases like this the methods to the facade are usually in terms of simple, primitve operations on the objects involved. In more complex cases, you need a layer of helper objects that are used to transfer information between the client and the facade.
This approach is most useful when you want to use an alternate means of persisting data rather than using CMP. If you want to store information in CICS or IMS, or any other form of data storage, this is the only approach that is valid. Bean Managed persistence isnot the best approach for these kinds of data representations at this time due to the insufficient support of these systems for XA transactions.
© Copyright IBM Corporation 1999
Helper Objects
+HOSHU�2EMHFWV�DFW�DV�LQWHUPHGLDULHV�EHWZHHQ�WKH�)DFDGH�DQG�WKH�&OLHQW5HSUHVHQW�EXVLQHVV�REMHFW�GDWD$UH�QRW�WUDQVDFWLRQDO0LQLPL]H�GLVWULEXWLRQ��FURVV�VHFWLRQ�
7KH\�DUH�FRSLHG�E\�YDOXH�IURP�(-%�VHUYHU�WR�FOLHQW7KH\�PXVW�EH�GHFODUHG�6HULDOL]DEOH
85
Any object that is serializable can be returned as the value of a Session bean’s method. This object will be serialized and copied across the network to the requesting client. Likewise modified helper objects can be passed back to the session bean in the same way.
Remember that all accesses to an EJB are network calls. This can adversely affect performance when you have a large number of fine-grained objects that you access from the client. Using helper objects can reduce this network traffic. The client can make several changes at once to a single helper object and then send that helper object to the facade as one network call instead of several -- one for each change.
© Copyright IBM Corporation 1999
Facades and Helpers
helper objects
serialized andsent over IIOP
EJB ServerEJB Client
helper objects
Fac
ade EJBs and
other data sources
ViewCode
ControlCode
Control code modifies the helpersaccording to the user's actions.
The facade creates the helpers fromthe EJB's (or other data sources) andlikewise updates them from the changedhelper objects received.
86
© Copyright IBM Corporation 1999
Command Architectures
6HQGLQJ�HQWLUH�KHOSHU�REMHFWV�DFURVV�WKH�QHWZRUN�FDQ�EH�SUREOHPDWLF<RX�GRQW�QHFHVVDULO\�ZDQW�WR�FKDQJH�HYHU\�ILHOG�HYHU\�WLPH
7KH�&RPPDQG�SDWWHUQ�FDQ�UHGXFH�WKLV�FRPSOH[LW\$�FRPPDQG�LV�D�UHLILFDWLRQ�RI�DQ�DFWLRQ,QVWHDG�RI�VHQGLQJ�WKH�FKDQJHG�REMHFWV��VHQG�D�GHVFULSWLRQ�RI�ZKDW�KDSSHQHG�WR�WKHP
87
Command is one of the design patterns from Gamma et. al. A Command is an object that represents an action on another object. It usually has a method (like "run") that carries out that action on the target object. Many different classes of commands may represent different actions, or commands may be parameterized by what they do (and may use reflection to execute their actions).
© Copyright IBM Corporation 1999
Commands in Action
helper objects
serialized andsent over IIOP
EJB ServerEJB Client
commands
Fac
ade EJBs and
other data sources
ViewCode
ControlCode
Control code modifies the helpersaccording to the user's actions.
The facade creates the helpers fromthe EJB's (or other data sources) andlikewise updates them from the commands that are received.
88
This architecture is used in the JStore example that is available from the IBM website.
© Copyright IBM Corporation 1999
Pros and Cons of Session Facades
$GYDQWDJHV�RI�VHVVLRQ�PDQDJHPHQW(QWLW\�EHDQV�UHPDLQ��SXUH��EXVLQHVV�ORJLF6HVVLRQ�EHDQV�FDQ�WLH�WRJHWKHU�PXOWLSOH�GDWD�VRXUFHV6RPH�WUDQVDFWLRQ�PDQDJHPHQW�WKURXJK�6HVVLRQ6\QFKURQL]DWLRQ
'LVDGYDQWDJHV�RI�VHVVLRQ�PDQDJHPHQW7KH�REMHFW�PRGHO�LV�PRUH�FRPSOH[
89
© Copyright IBM Corporation 1999
Enterprise Java Future directions
;0/�ZLWK�-060HVVDJHV�ZLOO�EH�IRUPDWWHG�;0/�WH[W6PDUWV�ZLOO�EH�DGGHG�ZLWK�ILOWHULQJ��HWF�7KH�GLUHFWLRQ�IRU�����������
6RPH�(-%�&KDQJHV(YHQWV�DQG�7ULJJHUV+DOI�(-%V��'HSHQGHQW�2EMHFWV�7KH�'LVFRQQHFWHG�&OLHQW�3UREOHP:LOO�UHDOO\�KLW�LQ����������
90
© Copyright IBM Corporation 1999
Summary
(QWHUSULVH�-DYD�LV�ZKHUH�WKH�DFWLRQ�ZLOO�EH�LQ�-DYD6HUYOHWV�DQG�-63V�PDNH�XS�WKH�9LHZ�DQG�&RQWUROOHU�IRU�+70/2U�8VH�$SSOHWV�RU�$SSOLFDWLRQV�WR�PDNH�XS�WKH�9&�OD\HU
(-%V�DUH�WKH�EDFN�HQG�UHXVDEOH�EXVLQHVV�PRGHOV-06�SURYLGHV�FRQQHFWLYLW\�WR�EDFN�HQG�V\VWHPV
91