7 Replies Latest reply on Apr 24, 2007 3:38 PM by kurtstam

    Passing parameters to custom esb actions

    cjunge

      Hi, I need to pass several parameters of different types to actions so I am using an Object array.

      Before I send a message to an action (through TwoWayCourrier) I this:

      // 1: in sender (it's an esb aware ws actually), params is an object array
      Message requestMessage = MessageFactory.getInstance().getMessage(MessageType.JBOSS_XML);
      requestMessage.getBody().setContents(toBytes(params));


      When the action receives the message it casts the result of decoding the message to an Object array and accesses it's values normally.

      // 2: in action
      Object [] params = (Object []) toObject(message.getBody().getContents());


      When I set the esb message content I use this toBytes function.

      public static byte[] toBytes(Object object) throws Exception {
       ByteArrayOutputStream baos = new ByteArrayOutputStream();
       ObjectOutputStream oos = new ObjectOutputStream(baos);
       oos.writeObject(object);
       return baos.toByteArray();
      }


      In the esb action I am decoding the message content using this toObject function:

      public static Object toObject(byte[] bytes) throws Exception {
       Object object = null;
       object = new ObjectInputStream(
       new ByteArrayInputStream(bytes)).readObject();
       return object;
      }


      This worked ok with esb 4.0 but now I'm using the 4.2MR1 version and I get the following exception when the action tries to obtain the object array from the message content.

      org.jboss.soa.esb.actions.ActionProcessingException: Unexpected invocation target exception from processor
       at org.jboss.soa.esb.listeners.message.ActionProcessorMethodInfo.processMethods(ActionProcessorMethodInfo.java:123)
       at org.jboss.soa.esb.listeners.message.OverriddenActionLifecycleProcessor.process(OverriddenActionLifecycleProcessor.java:74)
       at org.jboss.soa.esb.listeners.message.ActionProcessingPipeline.process(ActionProcessingPipeline.java:206)
       at org.jboss.soa.esb.listeners.message.MessageAwareListener$1.run(MessageAwareListener.java:300)
       at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:650)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:675)
       at java.lang.Thread.run(Thread.java:595)
      Caused by: java.lang.ClassCastException: java.lang.String
       at cl.falabella.met.VentasAction.GetVentas(VentasAction.java:57)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.jboss.soa.esb.listeners.message.ActionProcessorMethodInfo.processMethods(ActionProcessorMethodInfo.java:102)
       ... 6 more


      The line the cast exception reports is the code labeled 2.

      I'm not sure if this could be related to some change introduced in the 4.2 version or if I am making some mistake. I would appreciate any comments on this issue.

      Thanks,
      Cristian


        • 1. Re: Passing parameters to custom esb actions
          kurtstam

          Hi Christian,

          I don't know of any change that may cause this. Are you sure you're listing all the code involved, It's complaining about a string classcast:


          Caused by: java.lang.ClassCastException: java.lang.String
          at cl.falabella.met.VentasAction.GetVentas(VentasAction.java:57)


          which is hard for my to figure out where in your code this happens.

          --Kurt

          • 2. Re: Passing parameters to custom esb actions
            cjunge

            Thanks for the response Kurt, the line it complains about is:

            Object [] params = (Object []) toObject(message.getBody().getContents());

            Which is weird because there is no String casting involved.

            • 3. Re: Passing parameters to custom esb actions
              kconner

               

              "cjunge" wrote:
              Thanks for the response Kurt, the line it complains about is:

              Object [] params = (Object []) toObject(message.getBody().getContents());

              Which is weird because there is no String casting involved.

              Your toObject method must be returning String if this is indeed the line corresponding to the exception. Do you override/overload the method?

              • 4. Re: Passing parameters to custom esb actions
                kurtstam

                I think it maybe safer to use "named" objects in the body. So do an "add" and then later a "get" with the same parameter name. getContents() is intented to get the entire content in a byte[]. No need to use that here.

                • 5. Re: Passing parameters to custom esb actions
                  estebanschifman

                  Cristian,
                  If you're passing an Object[], you might want to consider using body.add("myName",myObj).
                  (and body.get("myName") at the other end of the wire) There would be no need for class casting. Just keep in mind that whatever travels in a Message needs to be Serializable.

                  • 6. Re: Passing parameters to custom esb actions
                    cjunge

                    Using the add and get functions to transmit the Object[] worked fine.

                    I didn't want to pass every element of the array as a different parameter (different add and get) because I have one piece of code that does the comunicaction with different actions so if I had done that I would have had to rewrite the same code many times.

                    Thank you all for your help.

                    • 7. Re: Passing parameters to custom esb actions
                      kurtstam

                      You should be able to pass in the entire Collection as one parameter. It is an Object after all.