+ All Categories
Home > Documents > Java EE and EJBs - UPapm/TDIN/docs/09ejbs.pdf · Java EE and EJBs Distributed Components in Java...

Java EE and EJBs - UPapm/TDIN/docs/09ejbs.pdf · Java EE and EJBs Distributed Components in Java...

Date post: 12-Aug-2019
Category:
Upload: phamque
View: 220 times
Download: 1 times
Share this document with a friend
16
JavaEE and EJBs Java EE and EJBs Distributed Components in Java APM@FEUP
Transcript
Page 1: Java EE and EJBs - UPapm/TDIN/docs/09ejbs.pdf · Java EE and EJBs Distributed Components in Java APM@FEUP. JavaEE and EJBs 2 General architecture of Java EE Java EE –Framework for

JavaEE and EJBs

Java EE and EJBs

Distributed Components in Java

APM@FEUP

Page 2: Java EE and EJBs - UPapm/TDIN/docs/09ejbs.pdf · Java EE and EJBs Distributed Components in Java APM@FEUP. JavaEE and EJBs 2 General architecture of Java EE Java EE –Framework for

JavaEE and EJBs 2

General architecture of Java EE

Java EE – Framework for the development of 3 - tier

distributed applications in Java

Resource tier

DBMSs

Message

Queues

Connectors

Brokers

Logic Tier

Session

Beans

Web

Client

Rich client

application

Presentation Tier

Web Container

EJB Container

Client app container

Application Server

Message

Driven

Beans

Entity

Classes

Servlets

JSP

JSF

WS

Resources

APM@FEUP

Page 3: Java EE and EJBs - UPapm/TDIN/docs/09ejbs.pdf · Java EE and EJBs Distributed Components in Java APM@FEUP. JavaEE and EJBs 2 General architecture of Java EE Java EE –Framework for

JavaEE and EJBs 3

Logic component types (EJBs)

Session Beans – Objects implementing methods for the business logic. It’s the object directly called by the presentation layer

Message-driven Beans – Logic invoked in asynchronous architectures through JMS messages. Usually appear as intermediaries between the clients calling

them asynchronously and session beans

Entities – Persistence objects representing the information needed for the business logic (as objects). They implement a connection to a database and execute the

mapping between relational tables and their vision as objects

They were considered as EJBs and used to run in an EJB container in old Java EE versions

After Java EE version 1.5 they are independent and constitute an autonomous specification and API (JPA).

APM@FEUP

Page 4: Java EE and EJBs - UPapm/TDIN/docs/09ejbs.pdf · Java EE and EJBs Distributed Components in Java APM@FEUP. JavaEE and EJBs 2 General architecture of Java EE Java EE –Framework for

JavaEE and EJBs 4

EJB types

CMP – Container managed persistence (automatic)

BMP – Bean managed persistence

Synchronous Asynchronous

EJBs

Message

Driven

SessionEntities

CMP BMP Stateless Stateful Singleton

APM@FEUP

Page 5: Java EE and EJBs - UPapm/TDIN/docs/09ejbs.pdf · Java EE and EJBs Distributed Components in Java APM@FEUP. JavaEE and EJBs 2 General architecture of Java EE Java EE –Framework for

JavaEE and EJBs 5

Usage of the several bean types

Client

DB

MDB

Entity

Session

Bean

Queue

Asynchronous communication

Synchronous communication

Application

Server

APM@FEUP

Page 6: Java EE and EJBs - UPapm/TDIN/docs/09ejbs.pdf · Java EE and EJBs Distributed Components in Java APM@FEUP. JavaEE and EJBs 2 General architecture of Java EE Java EE –Framework for

JavaEE and EJBs 6

EJB interfaces

External

Client

JVM

EJB

container

EJB

Remote

Interface

Local

Interface

WS

Endpoint

WS

Local

ClientWeb Service

Client

IS

APM@FEUP

Page 7: Java EE and EJBs - UPapm/TDIN/docs/09ejbs.pdf · Java EE and EJBs Distributed Components in Java APM@FEUP. JavaEE and EJBs 2 General architecture of Java EE Java EE –Framework for

JavaEE and EJBs 7

A session EJB

import javax.ejb.Local;

@Local

public interface NameBeanLocal {

….. local methods …..

}

import javax.ejb.Remote;

@Remote

public interface NameBeanRemote {

….. remote methods …..

}

import javax.ejb.Stateless;

@Stateless

public class NameBean implements NameBeanLocal,

NameBeanRemote {

public NameBean () {

}

….. interfaces implementation …..

}Annotations

Resource usage

@Resource

Interceptions

@PostConstruct

@PreDestroy

@PreActivate (stateful)

@PrePassivate (stateful)

@Remove (stateful)

import javax.ejb.Stateful;

@Stateful

import javax.ejb.Singleton;

@Singleton

APM@FEUP

Page 8: Java EE and EJBs - UPapm/TDIN/docs/09ejbs.pdf · Java EE and EJBs Distributed Components in Java APM@FEUP. JavaEE and EJBs 2 General architecture of Java EE Java EE –Framework for

JavaEE and EJBs 8

Java EE resources clients

JNDI – Java Naming and

Directory Interface

registry of the several

available resources

in a Java EE

application server

can be queried for

the resources

In recent Java EE

versions, it is hidden

behind the mechanism

of injection

APM@FEUP

Page 9: Java EE and EJBs - UPapm/TDIN/docs/09ejbs.pdf · Java EE and EJBs Distributed Components in Java APM@FEUP. JavaEE and EJBs 2 General architecture of Java EE Java EE –Framework for

EJB clients

JavaEE and EJBs 9

Execute in a special environment, designated as “Application Client

Container”

Communicate with the server using RMI or RMI-IIOP and the JNDI service

import javax.naming.*;

@EJB NameBean nb;

…..

nb. …..

….

Using annotations:

import javax.naming.*;

NameBean nb;

…..

InitialContext ic;

ic = new InitialContext();

nb = (NameBean) ic.lookup(

NameBean.class.getName() );

….

nb. …..

….

Explicit lookup:

Execute with “appclient –client ….jar” (in the Glassfish server)

APM@FEUP

Page 10: Java EE and EJBs - UPapm/TDIN/docs/09ejbs.pdf · Java EE and EJBs Distributed Components in Java APM@FEUP. JavaEE and EJBs 2 General architecture of Java EE Java EE –Framework for

Installation in an Application Server

JavaEE and EJBs 10

Depends from the particular Application Server (here using Glassfish):

Compilation:

javac -classpath %GLASSFISH_HOME%\lib\javaee.jar -d ./classes *.java

Packaging (putting the EJB classes in a .jar archive):

jar cvf …..

Deploying:

%GLASSFISH_HOME%\bin\asadmin deploy --host localhost --port 4848 --user admin

--passwordfile %GLASSFISH_HOME%\passfile --upload=true --target server

…….\NameBean.jar

When compiling the client you need to put in the classpath:

javaee.jar (from the AS) and the classes referenced from the EJBs

Running:

%GLASSFISH_HOME%\bin\appclient -client ….\Client.jar

EJB

client

APM@FEUP

Page 11: Java EE and EJBs - UPapm/TDIN/docs/09ejbs.pdf · Java EE and EJBs Distributed Components in Java APM@FEUP. JavaEE and EJBs 2 General architecture of Java EE Java EE –Framework for

Stateless Session EJBs as WS

JavaEE and EJBs 11

@WebService

@WebMethod

Import javax.ejb.Stateless;

Import javax.jws.WebMethod;

Import javax.jws.WebService;

@Stateless

@WebService

Public class ……… {

public ……… () {

}

@WebMethod

public ………… ( … ) {

}

}

Use the annotations for the class and methods

Client (via proxy):

In glassfish:

Build proxy with the wsimport command:

%GLASSFISH_HOME%\bin\wsimport -keep -d ….. http:// …. ?wsdl

WSSessionBeanService service = new WSSessionBeanService();

WSSessionBean port = service.getWSSessionBeanPort();

String result = port.hello("world");

Obtaining a proxy instance

@WebServiceRef(wsdlLocation = "http:// …. ?wsdl")

private static WSSessionBeanService service;

WSSessionBean port = service.getWSSessionBeanPort();

String result = port.hello("world");

Inside an “application client container”:

APM@FEUP

Page 12: Java EE and EJBs - UPapm/TDIN/docs/09ejbs.pdf · Java EE and EJBs Distributed Components in Java APM@FEUP. JavaEE and EJBs 2 General architecture of Java EE Java EE –Framework for

Messages in EJBs

JavaEE and EJBs 12

The message services in Java use the JMS (Java Messaging Service)

specification – implemented by several suppliers and ASs

• Publisher/Subscriber

Publisher Topic

Subscriber

Subscriber

JMS

• Point to point

Sender Queue

Receiver

Receiver

JMS

MDBs can work with the two models

APM@FEUP

Page 13: Java EE and EJBs - UPapm/TDIN/docs/09ejbs.pdf · Java EE and EJBs Distributed Components in Java APM@FEUP. JavaEE and EJBs 2 General architecture of Java EE Java EE –Framework for

JavaEE and EJBs 13

The MDB@MessageDriven(mappedName = "jms/TestQueue", activationConfig = {

@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),

@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") })

public class QueuedBean implements MessageListener {

public QueuedBean() { }

public void onMessage(Message message) {

if (message instanceof TextMessage) {

TextMessage msg = (TextMessage) message;

String stMsg = msg.getText();

...

}

}

} Message types:

ByteMessage - byte array (manual serialization)

MapMessage - set of name/value pairs

ObjectMessage - any object (serializable)

StreamMessage - sequences of primitive type values

TextMessage - a string

APM@FEUP

Page 14: Java EE and EJBs - UPapm/TDIN/docs/09ejbs.pdf · Java EE and EJBs Distributed Components in Java APM@FEUP. JavaEE and EJBs 2 General architecture of Java EE Java EE –Framework for

JavaEE and EJBs 14

The MDB client

@Resource(name = "jms/TestQueue")

private static Queue testQueue;

@Resource(name = "jms/TestQueueFactory")

private static ConnectionFactory testQueueFactory;

Connection connection = null;

Session session = null;

Message msg = null;

connection = testQueueFactory.createConnection();

session = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);

MessageProducer messageProducer = session.createProducer(testQueue);

msg = session.get...Message();

...

messageProducer.send(msg);

APM@FEUP

Page 15: Java EE and EJBs - UPapm/TDIN/docs/09ejbs.pdf · Java EE and EJBs Distributed Components in Java APM@FEUP. JavaEE and EJBs 2 General architecture of Java EE Java EE –Framework for

JavaEE and EJBs 15

Services of an Application Server

Object pooling (object management)

Transactions in entity beans and MDBs

Persistence – using stateful and singleton session

beans and also entity beans (may be automatic or

manual)

Security – can be oriented to the exposed methods

Messaging – using message driven beans

APM@FEUP

Page 16: Java EE and EJBs - UPapm/TDIN/docs/09ejbs.pdf · Java EE and EJBs Distributed Components in Java APM@FEUP. JavaEE and EJBs 2 General architecture of Java EE Java EE –Framework for

JavaEE and EJBs 16

An application example

A banking application

ATM

Web Banking

Bank teller

or

APM@FEUP


Recommended