3 Replies Latest reply on Sep 11, 2006 7:11 AM by galo.navarro

    Entity EJBs, @Service & Thread

    galo.navarro

      Hi, I'm trying to get a service running on my JBoss server that would awake every now and then, get a list of entity beans and throw a message to a JMS queue for each of those items, and sleep for a while.

      If I strip the bit loading Entity beans off, everything works, I send a test message to the queue, etc.

      But if I do the same loading the beans, I get the exception below. I guess I'm doing something wrong but cannot figure out what.

      17:02:32,650 ERROR [STDERR] Exception in thread "Thread-16"
      17:02:32,650 ERROR [STDERR] java.lang.NullPointerException
      17:02:32,650 ERROR [STDERR] at org.jboss.ejb3.entity.ManagedEntityManagerFactory.getNonTxEntityManager(ManagedEntityManagerFactory.java:58)
      17:02:32,650 ERROR [STDERR] at org.jboss.ejb3.entity.ManagedEntityManagerFactory.getTransactionScopedEntityManager(ManagedEntityManagerFactory.java:163)
      17:02:32,650 ERROR [STDERR] at org.jboss.ejb3.entity.TransactionScopedEntityManager.createNamedQuery(TransactionScopedEntityManager.java:132)
      17:02:32,650 ERROR [STDERR] at tal.noc.beans.NOCManagerService.loadNOCs(NOCManagerService.java:79)
      17:02:32,650 ERROR [STDERR] at tal.noc.beans.NOCManagerService.run(NOCManagerService.java:86)
      17:02:32,650 ERROR [STDERR] at java.lang.Thread.run(Unknown Source)
      


      This is my service:

      @Service(objectName="jboss:custom=NOCManagerService")
      public class NOCManagerService implements Runnable, NOCManagementService {
      
      ....
      
      public void run() {
       this.running=true;
       while(this.running) {
       System.out.println("Awaken");
       Iterator nocs=this.loadNOCs().iterator();
       while(nocs.hasNext()) {
       NOC noc = (NOC)nocs.next();
       try {
       MessageSender ms=new MessageSender();
       ms.sendMessage("NOC: " + noc.toString(), "queue/msgQueue");
       } catch (Exception e) {
       e.printStackTrace(System.out);
       }
       }
       try {
       MessageSender ms=new MessageSender();
       ms.sendMessage("NOC", "queue/msgQueue");
       } catch (Exception e) {
       e.printStackTrace(System.out);
       }
       try {
       synchronized (this) {
       wait(60*1000);
       }
       } catch (Exception e) {
       e.printStackTrace(System.out);
       }
       }
       }
      


      The service implements the following interface, just in case it makes any difference

      @Management
      public interface NOCManagementService {
      


      Thanks a lot.

        • 1. Re: Entity EJBs, @Service & Thread
          galo.navarro

          Any ideas please?

          • 2. Re: Entity EJBs, @Service & Thread
            wolfc

            How did you start the thread?

            I suspect that you're running a custom thread which has no transaction context. You could try using the EJB Timer Service, it starts business methods within the correct context.

            • 3. Re: Entity EJBs, @Service & Thread
              galo.navarro

               

              "wolfc" wrote:
              How did you start the thread?

              I suspect that you're running a custom thread which has no transaction context. You could try using the EJB Timer Service, it starts business methods within the correct context.


              In the start() method of my service:

               public void start() throws Exception {
               System.out.println("start");
               Thread thread = new Thread(this);
               thread.start();
               }
              



              I had considered using a Timer for it but would that not be much more unnefficient using a Service for something that doesn't quite need it?

              Thanks.