3 Replies Latest reply on Aug 27, 2012 9:48 AM by rcernich

    Generics support for bean references

    jpechanec

      Hi,

       

      I was thinking about invocation of services with 'interface.esb' from Java code. Right now (if I understand it correctly) I need to create an interface with a single method and typed parameter and return value so I can do something like this in the code

       

       

      interface ServiceA {
           public MyClass process(String msg);
      }
      
      @Reference ServiceA service;
      
      MyClass o = service.process("this is message");
      

       

      Whenever I would need different types used (and have transformes behind the scenes do the work) I would need to define a new interface.

       

      Do you think that it would be useful feature to provide support for constructs like this?

       

       

      interface EsbService<I, O> {
           public O process(I msg)
      }
      
      @Reference EsbService<String, MyClass> serviceA;
      @Reference EsbService<String, String> serviceA2;
      @Reference EsbService<ClassA, ClassB> serviceB;
      

       

      J.

        • 1. Re: Generics support for bean references
          rcernich

          Hey Jiri,

           

          It should be easy enough to test out to see.  Keep in mind that references are injected based on the raw type if a name is not specified, so you should use the following form when you test:

           

           

          @Service(value=EsbService.class, name="ServiceA")
          public class ServiceA implements EsbService<String, MyClass> {
               public String process(MyClass msg) {
                    // do something
               }
          }
          
          
          @Reference("ServiceA") EsbService<String, MyClass> serviceA;
          @Reference("ServiceA2") EsbService<String, String> serviceA2;
          @Reference("ServiceB") EsbService<ClassA, ClassB> serviceB;
          

           

          When I get some time, I'll try to test it out myself.

           

          Best,

          Rob

          • 2. Re: Generics support for bean references
            jpechanec

            Hi Rob,

             

            sorry for not explaining it properly. I meant a little bit different scenario.

             

            Let's suppose we have 2 camel services, each with interface.esb interface. If I would want to call those services  from Java code using @Reference then I'd need to provide two Java interfaces that would conform to input/output message formats. My idea was to provide generalized interface that could be used for access to any such service - technically we can speak about access to BPM and other service implementations as well.

            • 3. Re: Generics support for bean references
              rcernich

              Hey Jiri,

               

              You do not need to declare distinct interfaces for each service.  The services themselves are named.  The default name for a service declared using @Service or injected using @Reference is the simple name of the class, but you can specify a name for the service, as in the code snippet above.  This is known to work.

               

              The only question is whether or not a service can be declared using generics (which I suspect is OK, but haven't actually tested).

               

              Best,

              Rob