Solved

Using timers with EJB3.0 entity beans

Posted on 2006-10-27
2
1,071 Views
Last Modified: 2008-03-17
I am attempting to use TimerService with an entity bean in an EJB3.0 application.  I have spent a couple of days looking for a way to get the EntityContext so that I can call EntityContext.getTimerService().  The problem is that EJB3.0 entity beans annotated with @Entity don't support @Resource injection.  I have tried implementing the setEntityContext() method but, given that I am not implementing javax.ejb.EntityBean, that unsuprisingly didn't work.  How do I get the EntityContext from an entity bean annotated with @Entity?  Is there a different way to get the TimerService?  My code follows:


@Entity
@Table(name = "periodic_script")
public class PeriodicScript {

    private static final long serialVersionUID = 1;

    // Entity bean properties
        .
        .
        .

    private EntityContext ctx;
      
    public void setEntityContext(EntityContext ec) {
          this.ctx = ec;
    }
   
        .
        .
        .

    @PostPersist
    public void scheduleScriptExecution() {
      ctx.getTimerService().createTimer(Calendar.getInstance().getTime(), getPeriod() * 1000, getId());
    }

    @Timeout
    public void executeScript(Timer timer) {
        // logic to execute when timeout occurs
            .
            .
            .
    }
      
    @PreRemove
    public void removeTimer() {
      for (Object timerObject : ctx.getTimerService().getTimers()) {
          Timer timer = (Timer) timerObject;
          if (timer.getInfo().equals(getId())) {
            timer.cancel();
          }
      }
    }
      
        .
        .
        .

}
0
Comment
Question by:bbkeppler
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 19

Accepted Solution

by:
ramazanyich earned 500 total points
ID: 17854172
On page 482 of EJB3 specification:
"Timers can be created for stateless session beans, message-driven beans, and 2.1 entity beans.
Timers cannot be created for stateful session beans[94] or EJB 3.0 entities."

So you can't use timer in class annotted by @Entity
0
 

Author Comment

by:bbkeppler
ID: 17857953
Thanks to ramazanyich for that answer.  I had perused the EJB 3.0 spec looking for the answer, but apparently I passed right over the relavent sentence.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
DNS 2008 standard 2 89
Instructions on how to install SCCM 6 131
questions about oracle weblogic cluster 4 185
Java charset encoding issue 8 143
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question