0

I want to print the string "qui" every 5 seconds. I have created an EJB singleton and through annotations I defined a timeout method. I expected that the string "qui" was printed every 5 seconds , but this does not happen. The string "qui" is printed in continuation. My Application server is Glassfish. Below there is my code:

import EJBData.AuctionFrontEndLocal;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.Timeout;
import javax.ejb.TimerConfig;
import javax.ejb.TimerService;

/**
 *
 * @author melix
 */
@Startup
@Singleton
public class Timer{

    @EJB
    private AuctionFrontEndLocal auctionFrontEnd;

    private Timer timer;

    @Resource TimerService tservice;

    @PostConstruct
    public void initTimer(){
      tservice.createIntervalTimer(0,5000,new TimerConfig());
    }

    @Timeout
    public void timeout() {
      System.out.println("QUI!");
    }

}
melix
  • 468
  • 4
  • 16
  • Could you use application server logs instead of println? – iku Jan 20 '15 at 09:39
  • Try deleting the tmp and data folders of your AS as an earlier version of your timer may mix things up. Also you can try deploying your code with a new project. I have seen this problem on JBoss EAP 6.2 and though I don't know the actual cause the same code that didn't work in one deployment works in another. – s.Daniel Apr 08 '15 at 13:31
  • Dear @Nunzio Meli , 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:39

2 Answers2

3

try it a bit simpler.

import javax.ejb.Schedule;
import javax.ejb.Singleton;
import org.slf4j.LoggerFactory;

@Singleton
public class Timer {
    public Timer () {
        LoggerFactory.getLogger(this.getClass()).info("Starting timer");
    }

    @Schedule (persistent = false, second = "*", minute = "*", hour = "*")
    private void second () {
        LoggerFactory.getLogger(this.getClass()).info("Second");
    }

    @Schedule (persistent = false, second = "*/2", minute = "*", hour = "*", info = "Second Second")
    private void secondSecond () {
        LoggerFactory.getLogger(this.getClass()).info("Second Second");
    }

    @Schedule (persistent = false, second = "*/3", minute = "*", hour = "*")
    private void thirdSecond () {
        LoggerFactory.getLogger(this.getClass()).info("Third Second");
    }
}
agelbess
  • 4,249
  • 3
  • 20
  • 21
0

your code seems ok... here is my working example:

package com.mycompany.mavenproject2;

import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.Timeout;
import javax.ejb.TimerConfig;
import javax.ejb.TimerService;

@Startup
@Singleton
public class NewSessionBean {

    @Resource
    private TimerService ts;

    @PostConstruct
    public void init() {
        final TimerConfig tc = new TimerConfig();
        tc.setPersistent(false);
        ts.createIntervalTimer(0, 5000, tc);
    }

    @Timeout
    public void timeout() {
        Logger.getLogger(NewSessionBean.class.getName()).severe("==> timeout called...");
    }

}

console output when i run it in glass fish 4.1

Information:   mavenproject2 was successfully deployed in 721 milliseconds.
Schwerwiegend:   ==> timeout called...
Schwerwiegend:   ==> timeout called...
Schwerwiegend:   ==> timeout called...
StefanHeimberg
  • 1,455
  • 13
  • 22
  • Dear @StefanHeimberg , 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:38