title: Setting Trusted Packages (ActiveMQ)
body: <p>I am trying to send receive ActiveMQ messages. But I see the messages come back with this message in them.</p>
  
  <pre><code>JMSException in onMessage(): javax.jms.JMSException: Failed to build body from content. Serializable class not available to broker. Reason: java.lang.ClassNotFoundException: Forbidden class com.logicalprovisioning.common.gtc.shared.GTCMessage! This class is not trusted to be serialized as ObjectMessage payload. Please take a look at http://activemq.apache.org/objectmessage.html for more information on how to configure trusted classes.
  </code></pre>
  
  <p>So I read the link in the message. And I tried to follow the instructions. Although I must say its not very well written on where to put the configuration.</p>
  
  <p>So what I did was:
  1. I edited the ACTIVEMQ_OPTS line in activemq.bat file in the bin folder to </p>
  
  <pre><code>if "%ACTIVEMQ_OPTS%" == "" set ACTIVEMQ_OPTS=-Xms1G -Xmx1G -Djava.util.logging.config.file=logging.properties -Djava.security.auth.login.config="%ACTIVEMQ_CONF%\login.config" -Dorg.apache.activemq.SERIALIZABLE_PACKAGES=com.logicalprovisioning.common.gtc.shared.GTCMessage
  </code></pre>
  
  <p>It didn't work.</p>
  
  <ol start="2">
  <li><p>I added the above line in the activemq.bat of the win64 folder as well. It didn't work.</p></li>
  <li><p>I modified my subscriber object creation to add the Trusted Packages. Like:</p>
  
  <pre><code>String providerEndpoints = "tcp://" + host + ":" + port + "?wireFormat.maxInactivityDuration=7200000";
  
  // Set the trusted packages/classes to move back and forth on the ActiveMQ JMS service.
  ArrayList&lt;String&gt; trustedClasses = new ArrayList&lt;String&gt;();
  
  trustedClasses.add("com.logicalprovisioning.common.gtc.shared.GTCMessage");
  
  // Obtain the factory
  ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
  
  activeMQConnectionFactory.setBrokerURL(providerEndpoints);
  
  // Add the trusted packages/classes to the ActiveMQ consumer.
  activeMQConnectionFactory.setTrustedPackages(trustedClasses);
  
  //Create the connection
  setConnection(activeMQConnectionFactory.createQueueConnection());
  getConnection().setClientID(this.getName());
  
  // Make a session
  setSession(getConnection().createQueueSession(false, Session.AUTO_ACKNOWLEDGE));
  
  getSession().createQueue(jmsDestination);
  
  // Create the destination
  Destination destination = getSession().createQueue(jmsDestination);
  
  String selector = "JMSCorrelationID = '" + getActionRequest().getOriginId() + "_" + getActionRequest().getRequestId() + "'" ;
  
  setConsumer(getSession().createConsumer(destination, selector));
  getConsumer().setMessageListener(new DefaultMessageListener(this));
  
  // Start ...
  gtcMessages = new GTCMessageQueue&lt;GTCMessage&gt;();  // We'll need a message store now
  getConnection().start();
  </code></pre></li>
  </ol>
  
  <p>And I added something similar in my producer as well for good measure:</p>
  
  <pre><code>    Context initialContext = new InitialContext();
          Context environmentContext = (Context) initialContext.lookup("java:comp/env");
  
          String queueConnectionFactoryNameLookup = PalInit.getProperty("jms.queue.connection.factory");
  
          // Set the trusted packages/classes to move back and forth on the ActiveMQ JMS service.
          ArrayList&lt;String&gt; trustedClasses = new ArrayList&lt;String&gt;();
  
          trustedClasses.add("com.logicalprovisioning.common.gtc.shared.GTCMessage");
  
          ActiveMQConnectionFactory activeMQConnectionFactory = (ActiveMQConnectionFactory) environmentContext.lookup(queueConnectionFactoryNameLookup);
  
          activeMQConnectionFactory.setTrustedPackages(trustedClasses);
          // Create connection
          QueueConnection queueConnection = activeMQConnectionFactory.createQueueConnection();
          queueConnection.start();
  
          // Create session and producer
          setSession(queueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE));
  
          String queueName = PalInit.getProperty("jms.destination");
          Queue jmsQueue = getSession().createQueue(queueName);
  
          setProducer(getSession().createProducer(jmsQueue));
          setQueueConnection(queueConnection);
  
          // Set Message "Time to Live" to the request timeout plus 10 minutes
          getProducer().setTimeToLive(getTimeout() + (10 * 60 * 1000L));
  </code></pre>
  
  <p>But nothing seems to work. I have the ActiveMQ-All jar in the Tomcat's lib folder and so too the jar where the GTCMessage class resides. Can anyone please tell me what am I doing wrong? Is it a problem of a missing class or my bad configuration? Any help would be appreciated. Thanks!</p>
  
  <p>The application is running on Tomcat 9, JAVA 1.8 and Active MQ 5.15.11.</p>