1 Reply Latest reply on Sep 29, 2009 5:18 AM by Francesco Marchioni

    synchronized @Service

    Florian Schmidt Newbie

      I want to build a caching singleton service with the jboss @Service annotation. In this singleton service the methods have to be synchronized. In my tests I saw, that the singleton instance of my service allows multiple active simultaneous threads (see bellow: Not synchronized output). When I mark the service methods with "synchronized", the service acts in the way I need it (see bellow: synchronized output).

      Her my questions:

      1. Is it allowed to use the synchronized keyword (EJB spec) in my @Service singleton methods?

      2. Are there any special jboss annotations for @Service singeltons, which I can use to mark service methods "SYNCHRONIZED"?

      thanks a lot,
      Florian


      public interface JBossServiceTest
      {
       public void test();
      }



      @Remote(JBossServiceTest.class)
      @Service
      public class JBossServiceTestBean implements JBossServiceTest
      {
       private static final Logger logger = Logger.getLogger(JBossServiceTestBean.class);
      
       private static int callCounter = 1;
      
       public JBossServiceTestBean()
       {
      
       }
      
       public void test()
       {
       int callId = callCounter++;
      
       logger.info("start test call " + callId);
      
       try
       {
       Thread.sleep(3000);
       }
       catch (Exception e)
       {
       logger.fatal("error :(", e);
       }
      
       logger.info("end test call " + callId);
      
       }
      }


      Not synchronized output:
      10:01:19,751 INFO [JBossServiceTestBean] start test call 1
      10:01:19,751 INFO [JBossServiceTestBean] start test call 8
      10:01:19,751 INFO [JBossServiceTestBean] start test call 9
      10:01:19,751 INFO [JBossServiceTestBean] start test call 6
      10:01:19,751 INFO [JBossServiceTestBean] start test call 7
      10:01:19,751 INFO [JBossServiceTestBean] start test call 3
      10:01:19,751 INFO [JBossServiceTestBean] start test call 2
      10:01:19,751 INFO [JBossServiceTestBean] start test call 5
      10:01:19,751 INFO [JBossServiceTestBean] start test call 1
      10:01:19,751 INFO [JBossServiceTestBean] start test call 4
      10:01:22,752 INFO [JBossServiceTestBean] end test call 1
      10:01:22,752 INFO [JBossServiceTestBean] end test call 9
      10:01:22,752 INFO [JBossServiceTestBean] end test call 7
      10:01:22,752 INFO [JBossServiceTestBean] end test call 4
      10:01:22,752 INFO [JBossServiceTestBean] end test call 1
      10:01:22,752 INFO [JBossServiceTestBean] end test call 5
      10:01:22,752 INFO [JBossServiceTestBean] end test call 2
      10:01:22,752 INFO [JBossServiceTestBean] end test call 3
      10:01:22,752 INFO [JBossServiceTestBean] end test call 6
      10:01:22,752 INFO [JBossServiceTestBean] end test call 8


      @Remote(JBossServiceTest.class)
      @Service
      public class JBossServiceTestBean implements JBossServiceTest
      {
       private static final Logger logger = Logger.getLogger(JBossServiceTestBean.class);
      
       private static int callCounter = 1;
      
       public JBossServiceTestBean()
       {
      
       }
      
       public synchronized void test()
       {
       int callId = callCounter++;
      
       logger.info("start test call " + callId);
      
       try
       {
       Thread.sleep(3000);
       }
       catch (Exception e)
       {
       logger.fatal("error :(", e);
       }
      
       logger.info("end test call " + callId);
      
       }
      }


      synchronized output:
      10:03:29,539 INFO [JBossServiceTestBean] start test call 1
      10:03:32,539 INFO [JBossServiceTestBean] end test call 1
      10:03:32,539 INFO [JBossServiceTestBean] start test call 2
      10:03:35,540 INFO [JBossServiceTestBean] end test call 2
      10:03:35,540 INFO [JBossServiceTestBean] start test call 3
      10:03:38,540 INFO [JBossServiceTestBean] end test call 3
      10:03:38,540 INFO [JBossServiceTestBean] start test call 4
      10:03:41,540 INFO [JBossServiceTestBean] end test call 4
      10:03:41,540 INFO [JBossServiceTestBean] start test call 5
      10:03:44,540 INFO [JBossServiceTestBean] end test call 5
      10:03:44,540 INFO [JBossServiceTestBean] start test call 6
      10:03:47,540 INFO [JBossServiceTestBean] end test call 6
      10:03:47,540 INFO [JBossServiceTestBean] start test call 7
      10:03:50,540 INFO [JBossServiceTestBean] end test call 7
      10:03:50,540 INFO [JBossServiceTestBean] start test call 8
      10:03:53,541 INFO [JBossServiceTestBean] end test call 8
      10:03:53,541 INFO [JBossServiceTestBean] start test call 9
      10:03:56,541 INFO [JBossServiceTestBean] end test call 9
      10:03:56,541 INFO [JBossServiceTestBean] start test call 10
      10:03:59,541 INFO [JBossServiceTestBean] end test call 10