Hi all,
Thanks for reading my posts.
Now, I would like to present some tips about Asynch implementation.
Let's see the following code below...
Environment:
Java EE 6/7 and Java SE 1.7/1.8
CDI 1.x
JBoss EAP/AS/Wildfly and so on...
- EJB Asynch
public interface CustomEvent extends Serializable {
/**
* Submit event execution
*
* @param event
*/
void submit();
}
public class DispatchEvent implements CustomEvent { private String name;
protected int age;
public DispatchEvent(String name, int age) { this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
@Override
public void submit() {
// builder pattern sample...see other posts about this pattern
StudentsBuilder studentsBuilder = new StudentsBuilder();
studentsBuilder.addStudent()
.getCurrentStudentPosition()
.defineName(this.name)
.defineAge(this.age)
.build()
.print("Hello!!");
}
}
public class Dispatch2Event extends DispatchEvent {
private String nickname;
public Dispatch2Event(String name, int age, String nickname) {
this.name = name;
this.age = age;
this.nickname = nickname;
}
public String getNickname() {
return this.nickname;
}
}
@Local
public interface StatelessEjbEventObserver extends Serializable {
/**
* Observe the event execution...
*
* @param event
*/
void onEjbAsynchEvent(@Observes DispatchEvent event);
void onEjbEvent(@Observes Dispatch2Event event);
}
@Stateless
public class EjbStatelessEjbEventObserverImpl implements StatelessEjbEventObserver {
/**
* serial
*/
private static final long serialVersionUID = 2264643954576089691L;
@Asynchronous
@Override
public void onEjbAsynchEvent(@Observes DispatchEvent<E> event) {
// do anything here...
event.submit(); // execution
System.out.println("Hello: " + event.getName()); }
@Override
public void onEjbEvent(@Observes Dispatch2Event<E> event) {
// do anything here...
event.submit(); // execution
System.out.println("Hello2: " + event.getNickname());
}
}
Pay attention if you need to work with Thread Safe...
The event will be fired from a CDI Object and after that the EJB will dispatch the "submit" method as an Asynchronous operation.
Take it easy to avoid overhead on performance stuff, if you are using transactions, because the container must lost the control over Asynch method.
- CDI Service
public class PersonService {
@Inject
private Event<DispatchEvent> dispatcher;
@Inject
private Event<Dispatch2Event> dispatcher2;
public dispatch(DispatchEvent event) {
dispatcher.fire(event);
}
public dispatch(Dispatch2Event event) {
dispatcher2.fire(event);
}
}
- Singleton EJB (Startup)
@Singleton
@Startup
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class EjbSingletonStartup {
//@Inject // repository implementation...access person data information
//private PersonRepository personRepo;
@Inject PersonService pService;
@PostConstruct
protected void init() throws InterruptedException, ExecutionException, TimeoutException {
//PersonLegacy p = personRepo.findById(0L); // ADMIN test sample
pService.dispatch(new DispatchEvent("alvesfred", 38));
pService.dispatch2(new Dispatch2Event("alvesfred", 38, "fred"));
}
}
So, it is just a sample concept and not a real implementation or production code.
In JEE 7, I advice you to use services like ManagedExecutorService (see the assumptions over JSR-236 to know more about concurrency/multithread).
Thanks a lot and regards.
No comments:
Post a Comment