javajunkie
javajunkie

Reputation: 117

Threading in JBoss

Okay so I have a java app being deployed to JBoss. The application needs to poll an external webservice for jobs. Each job can an should be run on its own thread. This will behave very similar to a jms queue with a pool of mdbs handling the messages. The only real difference is the queue of work is supplied by a restful webservice. So my question is what is the best way to start up my pool of threads or does jboss have a better way of doing this. JMS is not an option.

Upvotes: 1

Views: 3333

Answers (1)

Kazaag
Kazaag

Reputation: 2145

In theory you shouldn't manage your self thread in Java EE container (in practice it is a bit more relax, if you don't use EJB, JPA or any other container feature).

In Java EE 6 (JBoss 7) you can use Java EE features to do it; you need to leverage the timer feature with the new @Asynchronous EJB call. The only issue is that you don't have persistancy of the async call, if it fails (server crash during processing) the job is lost.

If you need to be sure the job is done, the only Java EE way is persistence JMS (even local one populated with the timer EJB)

Exemple:

@Singleton
@Startup
public class Pool{

    @Inject
    private AsyncBean aBean;

    @Resource
    private TimerService timerService;

    @Timeout
    public void pull(){
      try{
        // for performance you may get several job and launch several async job
        job = getJob() // make your REST call to get the job
        // make sure you realy get a job
        aBean.async(job)
      }finally{
        //recreate the timer
        timerService.createSingleActionTimer(100, new TimerConfig("timer name", false)); 
      }
    }

    @PostConstruct
    void init(){
       timerService.createSingleActionTimer(100, new TimerConfig("timer name", false));
       // as timer is created at each start up no need to persist it.
    }
 }

The async bean:

@Stateless
public class AsyncBean{
  @Asynchronous
  public void async(Job job){
     // do your stuff
  }
}

Upvotes: 1

Related Questions