14

Click here to load reader

Ejb3.1

Embed Size (px)

DESCRIPTION

new features of EJB3.1

Citation preview

Page 1: Ejb3.1

Whats new in EJB 3.1?a.k.a

How do I convertmy POJOs to EJBs with JEE6

Page 2: Ejb3.1

What I'm gonna talk about

● What is EJB 3.1● Whats new on EJB 3.1● Types of EJB 3.1

– Stateless

– Stateful

– Singleton

● Other aspects of EJB 3.1– Injecting beans in to another EJB

– Exposing EJB as REST

● Unit testing EJB

Page 3: Ejb3.1

What this session is NOT about

● Full JEE6 stack● Persistence with EJB ● Transactions● JMS● NOT about EJB but you I ll let u know about

EJB ;-)

Page 4: Ejb3.1

What is EJB 3.1

● A whole new simplified way of developing enterprise applications which follows JEE specification.

● “whole new simplified” -> just POJO no framework specific API needs to extended or realized always.

EJB == JB (the “E” factor is taken care by the container)

● Unit test without pain !!

Page 5: Ejb3.1

Whats new on EJB 3.1

● Requires NO framework specific API to be extended or realized

● Efficient use of Annotations for developing beans● No vendor specific deployment descriptors needed● New annotations to develop beans such as

– Singleton

– Startup

– Timer & schedule expressions

– Exposing EJB as REST services

Page 6: Ejb3.1

Stateless Bean

public class CalculatorBean {

public int add(int a, int b) {

return a + b;

}

public int subtract(int a, int b) {

return a - b;

}

}

Page 7: Ejb3.1

Stateless Bean

@statelesspublic class CalculatorBean {

public int add(int a, int b) {

return a + b;

}

public int subtract(int a, int b) {

return a - b;

}

}

Page 8: Ejb3.1

Stateful Beans

public class Counter {

private int count = 0;

public int count() {

return count;

}

public int increment() {

return ++count;

}

}

Page 9: Ejb3.1

Stateful Beans

@statefulpublic class Counter {

private int count = 0;

public int count() {

return count;

}

public int increment() {

return ++count;

}

}

Page 10: Ejb3.1

Singleton Beans

@singletonpublic class PropertyRegistry {

private final Properties properties = new Properties();

public String getProperty(String key) {

return properties.getProperty(key);

}

public String setProperty(String key, String value) {

return (String) properties.setProperty(key, value);

}

public String removeProperty(String key) {

return (String) properties.remove(key);

}

}

Page 11: Ejb3.1

Bootstrapping Beans on “startup”

@singleton

@startuppublic class PropertyRegistry {

private final Properties properties = new Properties();

public String getProperty(String key) {

return properties.getProperty(key);

}

public String setProperty(String key, String value) {

return (String) properties.setProperty(key, value);

}

public String removeProperty(String key) {

return (String) properties.remove(key);

}

}

Page 12: Ejb3.1

Injecting EJB to another

@statelesspublic class AuthenticationBean {

@EJB

private LoginBean loginBean;

public void doLogin() {

LoginBean.authenticate();

// ...

}

}

Page 13: Ejb3.1

Exposing EJB as REST

@Singleton

@Lock(LockType.WRITE)

@Path("/user")

@Produces(MediaType.APPLICATION_XML)

public class UserService {

@Path("/list")

@GET

public List<User> list() {

// ....

return users;

}

}

Page 14: Ejb3.1

JUnit tesing EJBs

public class CalculatorTest extends TestCase {

private CalculatorBean calculator;

protected void setUp() throws Exception {

EJBContainer ejbContainer = EJBContainer.createEJBContainer();

Object object = ejbContainer.getContext().lookup("java:global/simple- stateless/CalculatorBean"); assertTrue(object instanceof CalculatorBean);

calculator = (CalculatorBean) object;

}

public void testAdd() {

assertEquals(10, calculator.add(4, 6));

}