camel-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "BJ Peter DeLaCruz (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (CAMEL-5293) Sending map that contains a serialized object that is not a primitive type causes an exception to be thrown.
Date Sat, 19 May 2012 23:36:42 GMT

    [ https://issues.apache.org/jira/browse/CAMEL-5293?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13279632#comment-13279632
] 

BJ Peter DeLaCruz commented on CAMEL-5293:
------------------------------------------

Here is my test program:

**************************************************

package com.test; 

import java.io.Serializable; 
import java.util.Enumeration; 
import java.util.HashMap; 
import java.util.Map; 

import org.apache.activemq.camel.component.ActiveMQComponent; 
import org.apache.camel.CamelContext; 
import org.apache.camel.ProducerTemplate; 
import org.apache.camel.builder.RouteBuilder; 
import org.apache.camel.impl.DefaultCamelContext; 
import org.apache.log4j.Appender; 
import org.apache.log4j.ConsoleAppender; 
import org.apache.log4j.Level; 
import org.apache.log4j.Logger; 
import org.apache.log4j.PatternLayout; 

/** 
 * This program puts four different objects in the body of a Camel message. If 
 * the object is a map that contains an object that is not an objectified 
 * primitive (e.g. not an Integer), an exception is thrown. 
 * 
 * @author BJ Peter DeLaCruz <bjpeter@hawaii.edu>
 */ 
public class TestCamelJmsWithMap { 

  private static final Logger LOGGER = Logger.getLogger(TestCamelJmsWithMap.class); 

  private static final String DIRECT_URL = "direct:start"; 
  private static final String QUEUE_URL = "activemq:queue:myqueue"; 

  /** 
   * This program runs four different test cases, which are:<br>
   * <br>
   * <ol>
   * <li>A serialized object called TestObject.</li>
   * <li>A map of primitive types.</li>
   * <li>An object that wraps a map of TestObjects.</li>
   * <li>A map of TestObjects.</li>
   * </ol>
   * The first three test cases pass, but the last one fails.<br>
   * <br>
   * 
   * @param args None. 
   * @throws Exception If there are problems. 
   */ 
  public static void main(String... args) throws Exception { 
    // test(new TestObjectBodyFactory()); // This test case works. 
    // test(new PrimitiveMapBodyFactory()); // This test case works. 
    // test(new WrappedObjectBodyFactory()); // This test case works. 
    test(new TestObjectMapBodyFactory()); // This test case fails! 
  } 

  /** 
   * Sends an object in the body of a Camel message. 
   * 
   * @param bodyFactory The factory for creating the object to be used in the 
   * body. 
   * @throws Exception If there are problems. 
   */ 
  public static void test(BodyFactory bodyFactory) throws Exception { 
    Logger logger = Logger.getRootLogger(); 
    for (Enumeration<?> appenders = logger.getAllAppenders(); appenders.hasMoreElements();)
{ 
      Appender appender = (Appender) appenders.nextElement(); 
      if (appender instanceof ConsoleAppender) { 
        logger.removeAppender(appender); 
      } 
    } 
    ConsoleAppender console = new ConsoleAppender(new PatternLayout("%d [%t] %-5p %c - %m%n"),

        "System.out"); 
    logger.addAppender(console); 
    logger.setLevel(Level.INFO); 

    final CamelContext context = new DefaultCamelContext(); 
    context.addComponent("activemq", 
        ActiveMQComponent.activeMQComponent("vm://localhost?broker.persistent=false")); 

    context.addRoutes(new RouteBuilder() { 
      public void configure() throws Exception { 
        from(DIRECT_URL).to(QUEUE_URL); 
        from(QUEUE_URL).log("received message: ${body}"); 
      } 
    }); 
    context.start(); 

    ProducerTemplate producerTemplate = context.createProducerTemplate(); 
    while (true) { 
      try { 
        producerTemplate.sendBody(DIRECT_URL, bodyFactory.createBody()); 
      } 
      catch (Exception e) { 
        LOGGER.error("", e); 
      } 
      Thread.sleep(1000L); 
    } 
  } 

  /** A factory for creating objects to be used in test cases. */ 
  public static interface BodyFactory { 
    /** Creates the object used in the body of a Camel message. */ 
    public Object createBody(); 
  } 

  /** A factory for creating a map of primitives. */ 
  static class PrimitiveMapBodyFactory implements BodyFactory { 
    /** {@inheritDoc} */ 
    @Override 
    public Object createBody() { 
      Map<Integer, Integer> map = new HashMap<Integer, Integer>(); 
      map.put(1, 2); 
      return map; 
    } 
  } 

  /** A factory for creating a TestObject. */ 
  static class TestObjectBodyFactory implements BodyFactory { 
    /** {@inheritDoc} */ 
    @Override 
    public Object createBody() { 
      return new TestObject(); 
    } 
  } 

  /** A factory for creating a map of TestObjects. */ 
  static class TestObjectMapBodyFactory implements BodyFactory { 
    /** {@inheritDoc} */ 
    @Override 
    public Object createBody() { 
      Map<Integer, TestObject> map = new HashMap<Integer, TestObject>(); 
      map.put(1, new TestObject()); 
      return map; 
    } 
  } 

  /** A factory for creating an object that contains a map. */ 
  static class WrappedObjectBodyFactory implements BodyFactory { 
    /** {@inheritDoc} */ 
    @Override 
    public Object createBody() { 
      Map<Integer, TestObject> map = new HashMap<Integer, TestObject>(); 
      map.put(1, new TestObject()); 
      return new Wrapper(map); 
    } 
  } 

  /** The test object. */ 
  static class TestObject implements Serializable { 
    /** {@inheritDoc} */ 
    @Override 
    public String toString() { 
      return "This is a test."; 
    } 
  } 

  /** A wrapper that wraps an object. */ 
  static class Wrapper implements Serializable { 

    private Object contents; 

    /** 
     * Creates a new Wrapper. 
     * @param contents The object to wrap. 
     */ 
    public Wrapper(Object contents) { 
      this.contents = contents; 
    } 

    /** {@inheritDoc} */ 
    @Override 
    public String toString() { 
      return this.contents.toString(); 
    } 

  } 

}
                
> Sending map that contains a serialized object that is not a primitive type causes an
exception to be thrown.
> ------------------------------------------------------------------------------------------------------------
>
>                 Key: CAMEL-5293
>                 URL: https://issues.apache.org/jira/browse/CAMEL-5293
>             Project: Camel
>          Issue Type: Bug
>    Affects Versions: 2.9.0
>            Reporter: BJ Peter DeLaCruz
>
> I created a serialized object and put it in a map. I put the map in the body of a message,
and when I tried to send the message, an exception was thrown.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Mime
View raw message