EJB3 Timer in seam
jbeaken Apr 18, 2008 3:04 PMHi,
I have created a EJB3 Timer bean and registered it as a seam component, but all the other components that are suppose to be injected are null, ie no injection. Am I do anything wrong or has anyone got a working example
I initialise the timer by calling the start() methods, and then @Timeout annotates the method to be called, but this throws a null pointer when accessing any injected components
(in this case searchEngine)
thanks!
package com.ricall.services.timer.soundflavour.impl;
import java.util.Date;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.log.Log;
import com.ricall.domain.admin.SearchEngine;
import com.ricall.services.search.webservice.GetTrackSearchService;
import com.ricall.services.timer.soundflavour.SoundflavourHeartBeatService;
import com.sirensystems.SelectionResultSet;
@Stateless
@Name("soundflavourHeartBeatService")
public class SoundflavourHeartBeat implements SoundflavourHeartBeatService {
@Logger
private Log log;
@In(required = false, scope=ScopeType.APPLICATION)
@Out(required=false, scope=ScopeType.APPLICATION)
private SearchEngine searchEngine;
@In(required = false, scope=ScopeType.APPLICATION)
@Out(required=false, scope=ScopeType.APPLICATION)
private Boolean usingEmergencyDatabase;
@Resource
private TimerService timerService;
@In(create = true)
private GetTrackSearchService getTrackSearchService;
@In(required = false, scope = ScopeType.APPLICATION)
@Out(required = false, scope = ScopeType.APPLICATION)
private SoundflavourHeartBeatSearch soundflavourHeartBeatSearch;
@Timeout
public void timeoutHandler(Timer timer) {
try {
log.debug("---------------------");
log.debug("* Received Timer event: " + timer.getInfo() + searchEngine);
searchEngine = (SearchEngine) Contexts.getApplicationContext().get("searchEngine");
//If using database as default, don't check heartbeat
if(usingEmergencyDatabase == false && searchEngine == SearchEngine.DATABASE) {
log.debug(" Using database as default, not checking heart beat");
return;
}
if(hasHeartBeat()) {
log.debug("* Heart beat detected");
switchToSoundflavour();
} else {
log.debug("* No heart beat, switching to emergency database");
switchToEmergencyDatabase();
}
log.debug("---------------------");
}catch (Exception e) {
log.error("", e);
}
}
private boolean hasHeartBeat() {
try {
SelectionResultSet selectionResultSet = getSongsFromCombinedSearch();
if (selectionResultSet == null)
throw new NullPointerException();
return true;
} catch (Exception e) {
log.error("Soundflavour not responding", e);
return false;
}
}
private void switchToEmergencyDatabase() {
searchEngine = SearchEngine.DATABASE;
usingEmergencyDatabase = true;
}
private void switchToSoundflavour() {
usingEmergencyDatabase = false;
searchEngine = SearchEngine.WEBSERVICE;
}
public Timer start() {
System.out.println("Starting timer!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
return timerService.createTimer(new Date(
System.currentTimeMillis() + 50000), 1000,
"soundflavour heartbeat");
}
}