Search code examples
javatimerjax-ws

Webservice using jaxrs, starting a timer


I am trying to start a timer, but get a "Validation of the application resource model has failed during application initialization." I am using Jax-rs on tomcat.

@Path("/startGame")
public void startGame(){
    GameTimer timer = new GameTimer(1000);
    timer.start();
}

This is the GameTimer:

 public class GameTimer {

private Timer timer;
private long interval;

public GameTimer(long interval) {
    this.interval = interval;
}

/**
 * 
 */
public void start() {
    this.timer = new Timer();
    TimerTask task = new TimerTask() {
          @Override
          public void run() {
            new GameEvent(GameEvent.STEP).dispatch();
          };};
    timer.schedule(task, interval, interval);
}

public void setInterval(long interval) {
    this.interval = interval;
    stop();
    start();
}

/**
 * 
 */
public void stop() {
    timer.cancel();
}

/* (non-Javadoc)
 * @see java.lang.Object#toString()
 */
@Override
public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("GameTimer [timer=").append(timer).append("]");
    return builder.toString();
}
}

Solution

  • This is working here i have used a GET annotation which tells web service what to do.

    @Path("/{startGame}")
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    
    public String startGame(){
        GameTimer timer = new GameTimer(1000);
        timer.start();
        return "Timer Started";
    }
    

    Hope it helps !