Announcement Announcement Module
Collapse
No announcement yet.
@Header not working as I expect Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Header not working as I expect

    I am working on a spring integration project (1.0.2) and can't get the @Header annotation to work.

    Here's my service activator:

    Code:
        @Component
        public class MyTransformer {
            @ServiceActivator(inputChannel = "inChannel", outputChannel = "outChannel")
            public String transform(final Message<?> message,
                                    @Header(value = "eventId") final Long id,
                                    @Header(value = "error", required = false) final Throwable throwable) {
                return throwable == null
                        ? "event [" + eventId +"]"
                        : "event [" + eventId +"] error: " + throwable.getMessage();
            }
        }
    Here's the error message:



    Caused by: java.lang.IllegalArgumentException: Wrong number of arguments. Expected types {class java.lang.Object, class java.lang.Long, class java.lang.Throwable}, but received values {[Payload=parp][Headers={eventId=888Foo, error=java.lang.IllegalArgumentException: Invalid message content parp, springintegration_timestamp=1277803938244, springintegration_id=726d0769-bd27-4d95-a9ea-640c1f807065}]}.
    at org.springframework.integration.util.DefaultMethod Invoker.invokeMethod(DefaultMethodInvoker.java:71)
    at org.springframework.integration.handler.MessageMap pingMethodInvoker.doInvokeMethod(MessageMappingMet hodInvoker.java:160)
    at org.springframework.integration.handler.MessageMap pingMethodInvoker.invokeMethod(MessageMappingMetho dInvoker.java:107)
    at org.springframework.integration.handler.ServiceAct ivatingHandler.handleRequestMessage(ServiceActivat ingHandler.java:49)
    at org.springframework.integration.handler.AbstractRe plyProducingMessageHandler.handleMessageInternal(A bstractReplyProducingMessageHandler.java:91)
    at org.springframework.integration.handler.AbstractMe ssageHandler.handleMessage(AbstractMessageHandler. java:59)
    What am I doing wrong?

  • #2
    Looking at the stack trace its clear that the invocation expects 3 arguments:
    Expected types {class java.lang.Object, class java.lang.Long, class java.lang.Throwable}

    But you are invoking a method with only one argument which is a Message itself:
    but received values {[Payload=parp][Headers={eventId=888Foo, error=java.lang.IllegalArgumentException: In. . .

    I would also suggest to upgrade to the most current GA which is 1.0.4. The release you are using (1.0.2) is about 1.5 years old.

    Comment


    • #3
      I thought that Spring Integration would "magically" pull the two missing values out of the header map when invoking the method. Isn't that what the @Header annotation is supposed to do?

      Comment


      • #4
        With Spring Integration 2.0 we have a lot of interesting capabilities via SpEL, but you are using 1.0.2 which is very old.

        Second, it is really not an SI error, but rather general Java Reflection error. Internally it is getting a Method object for the method with 3 arguments, but you are passing only one with your invocation.

        Comment


        • #5
          Firstly, so I misremembered the SI version and we are indeed using 1.0.4 (we're not going to use non-GA release in production code)

          Secondly, I'm not invoking the method at all; I am sending a message on the "inChannel". Like I said, I thought that Spring Integration would see the @Header annotation and populate the invocation parameters from the header map.

          Comment


          • #6
            In 2.0 we do parameter mappings so you can do something like this:

            configuratuon:
            Code:
            <int:service-activator input-channel="input">
            	<bean class="thread91497.SampleService"/>
            </int:service-activator>
            service:
            Code:
            public void echo(@Header("firstName")String fname, @Header("lastName")String lname){
            		System.out.println(fname);
            		System.out.println(lname);
            }
            test:
            Code:
            Message message = MessageBuilder.withPayload("person").setHeader("firstName", "Betty").setHeader("lastName", "Page").build();
            channel.send(message);
            You can also use Spring Expression language as annotation value
            Code:
            public void echo(@Header("firstName.toUpperCase()")String fname, @Header("lastName")String lname){
            Last edited by oleg.zhurakousky; Jun 29th, 2010, 09:30 AM.

            Comment


            • #7
              ok, so I am officially an idiot.

              Event id header parameter should be a String, not a Long.

              Thanks, anyhow.

              Comment

              Working...
              X