2

I want to wake up ejb timer on every two second. For that I am creating ScheulerExpression as the code below...

package com.fks.nclp.ejb.timer.service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.LocalBean;
import javax.ejb.ScheduleExpression;
import javax.ejb.Startup;
import javax.ejb.Timeout;
import javax.ejb.TimerConfig;
import javax.ejb.TimerService;

import com.sun.jersey.spi.resource.Singleton;


@LocalBean
@Singleton
@Startup
public class HelloWorldService {


    @Resource
    TimerService timerService;

    @PostConstruct
    public void initialize(){
            System.out.println("EJB PRE CONSTRUCT");
            ScheduleExpression expression = new ScheduleExpression();

            //SET TIMER TO 2 SEC            
            expression.hour("*");
            expression.minute("*");
            expression.second("*/2");

            timerService.createCalendarTimer(expression,new TimerConfig("TimerScheduler",false));


    }


    @Timeout
    public void startTimer(){
        System.out.println("THIS IS EJB TIMER PROGRAM");
    }

}

But ... the EJB container is not waking up the scheduler after every two second.

Any Suggestion on this ? How can I make the expression to wake up every two second.

Thanks, Gunjan.

Máthé Endre-Botond
  • 4,826
  • 2
  • 29
  • 48
Gunjan Shah
  • 5,088
  • 16
  • 53
  • 72
  • Are you getting any logs/exceptions for method being invoked or it isn't being created at all. – Nayan Wadekar Jun 02 '12 at 18:25
  • The ScheduleExpression looks proper. What does Timer.getNextTimeout() return (via createCalendarTimer's return value)? What application server are you using? Your tag says ejb-3.0, but ScheduleExpression is EJB 3.1; does your application server version support EJB 3.1? – Brett Kail Jun 03 '12 at 14:56
  • I have used GlassFish 3.1 and sorry...I have added GlassFish server library which by default provides EJB3.1 – Gunjan Shah Jun 04 '12 at 05:13
  • There is not exception OR warning in the log. The problem is that its not initializing the bean at all. So post construct is not called. The sysout in the first line of initialize() function is not called .. – Gunjan Shah Jun 04 '12 at 05:55
  • Dear @Gunjan Shah , I have a problem: when I use `@Timeout` Icould not load the scheduler at startup even I use `@Startup`. please have a look at: http://stackoverflow.com/questions/42242037/parameterize-ejb-scheduler-with-schedule-expression – Hosein Aqajani Feb 22 '17 at 08:37

1 Answers1

3

There are two things which might be causing problem :

  1. The import statement seems incorrect, which is com.sun.jersey.spi.resource.Singleton, but should be javax.ejb.Singleton.

  2. Try removing @LocalBean annotation.

Nayan Wadekar
  • 11,444
  • 4
  • 50
  • 73
  • #1 one is almost certainly the problem, especially since OP clarified that `@PostConstruct` isn't being called at all. #2 is superfluous but harmless. – Brett Kail Jun 08 '12 at 17:29
  • Dear @Nayan Wadekar , I have a problem: when I use `@Timeout` Icould not load the scheduler at startup even I use `@Startup`. please have a look at: http://stackoverflow.com/questions/42242037/parameterize-ejb-scheduler-with-schedule-expression – Hosein Aqajani Feb 22 '17 at 08:36