1 SOA and Enterprise Computing Enterprise Java Beans.

42
1 SOA and Enterprise Computing Enterprise Java Beans

Transcript of 1 SOA and Enterprise Computing Enterprise Java Beans.

Page 1: 1 SOA and Enterprise Computing Enterprise Java Beans.

1

SOA and Enterprise Computing

Enterprise Java Beans

Page 2: 1 SOA and Enterprise Computing Enterprise Java Beans.

2

• Advanced Java 2 Platform How to Program by Deitel, Deitel, & Santry

• Thinking in Enterprise Java Bruce Eckel et. Al.

• EJB 3.0 API Sun documentation http://java.sun.com/products/ejb/docs.html

• Java Enterprise In A Nutshell Farley, Crawford & Flanagan

Some Useful Resources

Page 3: 1 SOA and Enterprise Computing Enterprise Java Beans.

3

From The Java EE TutorialServletsJSP’sAxis2HTML

Session beansEntity beansMessage Driven Beans

Page 4: 1 SOA and Enterprise Computing Enterprise Java Beans.

4

From The Java EE Tutorial

Page 5: 1 SOA and Enterprise Computing Enterprise Java Beans.

5

From The Java EE Tutorial

Page 6: 1 SOA and Enterprise Computing Enterprise Java Beans.

6

SOAP and Java EE

Slide from JAXM/JMS tutorial at Sun Microsystems

Page 7: 1 SOA and Enterprise Computing Enterprise Java Beans.

7

Slide from JAXM/JMS tutorial at Sun Microsystems

Page 8: 1 SOA and Enterprise Computing Enterprise Java Beans.

8

Benefits of Enterprise Beans(1)

• Simplify development of large, distributed applications.

• The developer can concentrate on business problems.

• The EJB container handles transaction management, security, and authorization.

• Portable – may be moved to other Java EE containers.

Page 9: 1 SOA and Enterprise Computing Enterprise Java Beans.

9

Benefits of Enterprise Beans(2)

• Scalable – the components may be distributed across many machines.

• Support for Location Transparency – the client need not be concerned with the location of the bean.

• Support for Transaction management - ensures data integrity (over concurrent access of shared resources).

• Promotes thin clients and web services.

Page 10: 1 SOA and Enterprise Computing Enterprise Java Beans.

10

Main Concept: Interposition

Application

stub skeleton

• examine security credentials• start or join transaction• call any necessary persistence functions• trigger various callbacks• call business logic• more work with transactions, persistence and callbacks• send back result or an exception

EJB Container

Page 11: 1 SOA and Enterprise Computing Enterprise Java Beans.

11

The Container Implements the component interface (From Eckel)

Page 12: 1 SOA and Enterprise Computing Enterprise Java Beans.

12

Enterprise Java Beans• Part of Java EE Enterprise Application Technologies• Currently at EJB3.0 (Big simplification of code with

annotations – less concern with configuration files)• Server-side managed components• Server-Side EJB objects may offer a remote view (via a remote procedure call protocol) a local view (direct procedure call) or both• Managed EJB container services are more involved than the plain old JVM• Components distributed in binary format and are configurable

Page 13: 1 SOA and Enterprise Computing Enterprise Java Beans.

13

Server-Side Implications• In order to pull off the RPC trick we need:

• A naming service -- Recall how RMI clients make requests on the

rmiregistry. -- A stand alone EJB client would use a global JNDI name. InitialContext ic = new InitialContext(); // provider properties Foo foo = (Foo) ic.lookup("FooEJB"); // and address of // naming service // found in // jndi.properties

• RPC proxies -- communications code along with the appropriate interface

Page 14: 1 SOA and Enterprise Computing Enterprise Java Beans.

14

Managed: EJB Container Services

• Object Persistence• Declarative Security Control• Declarative Transaction Control• Concurrency Management• Scalability Management

The programmerneed not worryabout the details.Think “JVM onsteroids”.

Page 15: 1 SOA and Enterprise Computing Enterprise Java Beans.

15

EJB Types

• Entity Beans• Session Beans• Message-Driven Beans

Page 16: 1 SOA and Enterprise Computing Enterprise Java Beans.

16

EJB Types

• Entity Beans• Session Beans

• Message-Driven Beans}

} RMI-based server side componentsAccessed using distributed objectProtocols (RMI IIOP)

New since EJB 2.0Asynchronous server sidecomponent that responds toJMS asynchronous messages(Think provider like JAXM)

Page 17: 1 SOA and Enterprise Computing Enterprise Java Beans.

17

Entity Beans (1)• Represent a row of a relation. O/R mapping.• Represent real world entities (customers,

orders, etc.).• Persistent objects typically stored in a

relational database using CMP (Container Managed Persistence) or BMP (Bean Managed Persistence).

• The client sees no difference between CMP and BMP beans.

• CMP promotes component portability (more reliant on the container to handle detail).

• CMP uses its own Query Language EJB QL.• CMP relies on an Abstract Schema in the

deployment descriptor.

Page 18: 1 SOA and Enterprise Computing Enterprise Java Beans.

18

Entity Beans (2)• Define a primary key class:

– Required for entity beans. – Provides a pointer into the database.– Must implement Java.io.Serializable.

• The EJB instance represents a particular row in the corresponding database table.

• The home interface for the entity EJB represents the table as a whole (has finder methods.)

Page 19: 1 SOA and Enterprise Computing Enterprise Java Beans.

19

Entity Beans (3)

• The primary key can be used by the client to locate the bean

• Rows may have columns that reference other entity beans (e.g. an order has a buyer)

• These relationships may be managed by the bean or by the container (container managed relationships)

Page 20: 1 SOA and Enterprise Computing Enterprise Java Beans.

20

Entity Bean(4) OrderEJB CusomerEJB

1 Many 1

Many

LineItemEJB ProductEJB

Many 1

A relationship field is like a foreign key in a database.If a b then a “knows about” or “holds a pointer to” b.

Page 21: 1 SOA and Enterprise Computing Enterprise Java Beans.

21

Session Beans• Are an extension of the client application.• Manage processes or tasks.• Are not persistent.• Often employ several different kinds of entity

beans.• Implement business logic.• May be used as a web service.• Come in two types:

– Stateless session beans (no memory between calls) purchase(severalIems,creditCardNo);– Stateful session beans (remember earlier calls) addToCart(item); purchase(creditCardNo);

Page 22: 1 SOA and Enterprise Computing Enterprise Java Beans.

22

Session Bean Quiz

• Which session bean promotes loose coupling?

– Stateless session beans (no memory between calls) purchase(severalIems,creditCardNo);– Stateful session beans (remember earlier calls) addToCart(item); purchase(creditCardNo);

Answer: Stateless session beans.

Page 23: 1 SOA and Enterprise Computing Enterprise Java Beans.

23

Implementing Entity and Session Beans

• Define the component interfaces– The remote interface specifies how the outside world

can access the bean’s business methods– The remote home interface specifies how the outside

world can access the bean’s life-cycle methods (for creating, removing and finding)

– The local interface specifies how the inside world (same EJB container) can access the bean’s business methods

– The local home interface specifies how the inside world can access the bean’s life-cycle methods

EJB 3.0 Makes this process much easier.

Page 24: 1 SOA and Enterprise Computing Enterprise Java Beans.

24

Message-Driven Beans (1)• Work in cooperation with Java Messaging System

(JMS).• JMS is an abstraction API on top of Message-Oriented

Middleware (MOM) – like JDBC is an abstraction API on top of SQL databases or like JAXR is an abstraction API on different types of XML registries or like JNDI is an abstraction API on directories.

• Each MOM vendor implements things differently.• MDB’s allow the developer to program using the

publish-subscribe messaging model based on asynchronous, distributed message queues.

• The MOM vendor need only provide a service provider for JMS (IBM’s MQSeries or Progress’ SonicMQ).

mm6
How does JMS work with JAXM 's provider?Can we run a Message Driven Bean when a SOAP document arrives?Research MQSeries and SonicMQ.
Page 25: 1 SOA and Enterprise Computing Enterprise Java Beans.

25

Message-Driven Beans (2)

• Are like session beans.• Have no persistent state. • Coordinate tasks involving other session

beans or entity beans.• Listen for asynchronous messages.• Unlike Session beans, provide no

remote interface describing the methods that can be called.

Page 26: 1 SOA and Enterprise Computing Enterprise Java Beans.

26

Message-Driven Beans (3)

• Are receivers of MOM messages coming through the JMS API.

• Usually take action when a message is received.

• Unlike session and entity beans, Message-Driven Beans expose no remote or local view. They are not called directly by a client.

Page 27: 1 SOA and Enterprise Computing Enterprise Java Beans.

27

Message-Driven Bean (4)

• Has no local, local home, remote, or remote home interfaces to define.

• The container will call the onMessage() method when an asynchronous message arrives. (Like JAXM message provider.)

• Extends the EnterpriseBean class and implements the javax.ejb.MessageDrivenBean and javax.jms.MessageListener interfaces

Page 28: 1 SOA and Enterprise Computing Enterprise Java Beans.

28

Message-Driven Bean (5)

• Two basic messaging-system models (1) point-to-point model allows messages to

be sent to a message queue to be read by exactly one message consumer.(2) publish/subscribe model allows components to publish messages on a topic to a server to be read by zero or more subscribers. Subscribers register for a topic.

Page 29: 1 SOA and Enterprise Computing Enterprise Java Beans.

29

In Both Messaging Models

• The messages hold: -- a header containing the destination and the sending time. -- message properties to allow the receiver to select which messages they would like to receive. These may be set by the sender. -- the message body itself.

Page 30: 1 SOA and Enterprise Computing Enterprise Java Beans.

30

Point-to-point on the Client Side

import javax.jms.*;

QueueConnection qCon;QueueSession qSes;QueueSender qSen;

Through JNDI get access to a QueueSender. Build messages and send them to the queue.The queue is responsible for transporting themessage to another queue on the server.

Page 31: 1 SOA and Enterprise Computing Enterprise Java Beans.

31

Point-To-Point on the Server Side

import javax.jms.*;

QueueConnection qCon;QueueSession qSes;QueueReceiver qRec;

Through JNDI get access to a QueueReceiver. Build a MessageListener with an onMessagemethod.

Page 32: 1 SOA and Enterprise Computing Enterprise Java Beans.

32

Message Driven Beans (6)

• Handles asynchronous messages.• Normally acts as a JMS message listener.• The message may have originated from an

application client, another enterprise bean, a web component or a non-Java application that can generate messages.

• Like a stateless session bean but with no interfaces.

• All operations within onMessage may be in a transaction context.

Page 33: 1 SOA and Enterprise Computing Enterprise Java Beans.

33

From Sun

Page 34: 1 SOA and Enterprise Computing Enterprise Java Beans.

34

On The Client // locate the connection factory and queue

connectionFactory =(ConnectionFactory) jndiContext.lookup("java:comp/env/jms/MyConnectionFactory");

destination =(Queue)

jndiContext.lookup("java:comp/env/jms/QueueName");

Page 35: 1 SOA and Enterprise Computing Enterprise Java Beans.

35

// Next, the client creates the queue connection, session, and sender:

connection = connectionFactory.createConnection();session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

messageProducer = session.createProducer(destination);

// Finally, the client sends several messages to the queue:

message = session.createTextMessage(); for (int i = 0; i < NUM_MSGS; i++) { message.setText("This is message " + (i + 1)); System.out.println("Sending message: " + message.getText()); messageProducer.send(message); }

Page 36: 1 SOA and Enterprise Computing Enterprise Java Beans.

36

JMS Message Types(1)The Message Body Contains:

TextMessage A java.lang.String object (for example, an XML document). MapMessage A set of name/value pairs, with names as String objects and values as primitive types in the Java programming language. The entries can be accessed sequentially by enumerator or randomly by name. The order of the entries is undefined.

Page 37: 1 SOA and Enterprise Computing Enterprise Java Beans.

37

JMS Message Types(2)BytesMessage A stream of uninterpreted bytes. This message type is for literally encoding a body to match an existing message

format.

StreamMessage A stream of primitive values in the Java programming

language, filled and read sequentially.

ObjectMessage A Serializable object in the Java programming language.

Page 38: 1 SOA and Enterprise Computing Enterprise Java Beans.

38

Listening to the Queuepublic void onMessage(Message inMessage) { TextMessage msg = null; try { if (inMessage instanceof TextMessage) { msg = (TextMessage) inMessage; System.out.println("MESSAGE BEAN: Message received: " +msg.getText()); } else { System.out.println("Message of wrong type: " + inMessage.getClass().getName()); } } catch (JMSException e) { e.printStackTrace(); mdc.setRollbackOnly(); } catch (Throwable te) { te.printStackTrace(); } }

Page 39: 1 SOA and Enterprise Computing Enterprise Java Beans.

39

Web Services

• A Web service client can access Java EE applications in two ways.

• First, the client can access a Web service created with JAX-RPC. Behind the scenes, JAX-RPC uses a servlet to implement the SOAP Web Service.

• Second, a Web service client can access a stateless session bean through the service endpoint interface of the bean. Other types of enterprise beans cannot be accessed by Web service clients.

Page 40: 1 SOA and Enterprise Computing Enterprise Java Beans.

40

A Stateless Session Bean as a Web Service

• The client need not know that its interacting with a Java EJB.

• It calls the bean like it calls any other web service.

• Thus, .NET interoperates with Java EE using XML on the wire.

Page 41: 1 SOA and Enterprise Computing Enterprise Java Beans.

41

The Web Service Endpoint Interface

package helloservice;

import java.rmi.RemoteException; import java.rmi.Remote;

public interface HelloIF extends Remote { public String sayHello(String name) throws RemoteException; }

The client cannotsee that it’s interactingwith an EJB

Page 42: 1 SOA and Enterprise Computing Enterprise Java Beans.

42

The Web Service Session Beanpackage helloservice;

import java.rmi.RemoteException; import javax.ejb.SessionBean;import javax.ejb.SessionContext;

public class HelloServiceBean implements SessionBean {

public String sayHello(String name) {

return "Hello " + name + "from HelloServiceEJB"; } public HelloServiceBean() {} public void ejbCreate() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sc) {}}

If we added remote and homeInterfaces then this bean couldalso be called using in the traditionalmanner – with remote references. No change to the bean would be necessary.

WSDL can be generated and allof the previous clients will work.