Click here to load reader
Upload
prasanna-kumar
View
707
Download
0
Embed Size (px)
DESCRIPTION
new features of EJB3.1
Citation preview
Whats new in EJB 3.1?a.k.a
How do I convertmy POJOs to EJBs with JEE6
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
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 ;-)
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 !!
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
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;
}
}
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;
}
}
Stateful Beans
public class Counter {
private int count = 0;
public int count() {
return count;
}
public int increment() {
return ++count;
}
}
Stateful Beans
@statefulpublic class Counter {
private int count = 0;
public int count() {
return count;
}
public int increment() {
return ++count;
}
}
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);
}
}
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);
}
}
Injecting EJB to another
@statelesspublic class AuthenticationBean {
@EJB
private LoginBean loginBean;
public void doLogin() {
LoginBean.authenticate();
// ...
}
}
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;
}
}
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));
}