Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Ejb3.1
1. Whats new in EJB 3.1?
a.k.a
How do I convert
my POJOs to EJBs with JEE6
2. 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
3. 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 ;-)
4. 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 !!
5. 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
6. 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;
}
}
7. Stateless Bean
@stateless
public class CalculatorBean {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
}
8. Stateful Beans
public class Counter {
private int count = 0;
public int count() {
return count;
}
public int increment() {
return ++count;
}
}
9. Stateful Beans
@stateful
public class Counter {
private int count = 0;
public int count() {
return count;
}
public int increment() {
return ++count;
}
}
10. Singleton Beans
@singleton
public 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);
}
}
11. Bootstrapping Beans on “startup”
@singleton
@startup
public 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);
}
}
12. Injecting EJB to another
@stateless
public class AuthenticationBean {
@EJB
private LoginBean loginBean;
public void doLogin() {
LoginBean.authenticate();
// ...
}
}
13. 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;
}
}