EJB3 Messagedrivenbean fires exception "This class is not Trusted to be Serialized as Objectmessage payload" when consuming an external Queue Activemq

Asked

Viewed 47 times

0

I’m using a message-driven bean (@MessageDriven) to consume a queue that is in an external Activemq configured in a Docker container, and in Objectmessage it is necessary to use a pojo (ArquivoRetornoDTO).

When the onmessage method is invoked, it is triggering the following exception when casting the objectmessage:

ClassNotFoundException: Forbidden class foo.bar.ArquivoRetornoDTO! 
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.

In the documentation in http://activemq.apache.org/objectmessage.html, there is the following example:

ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
factory.setTrustAllPackages(true);

But in my code I use a message-driven bean that way:

@MessageDriven(activationConfig = {
    @ActivationConfigProperty(propertyName = "destination", propertyValue = PreloadArquivoRetornoItemSinteticoQueue.JNDI_QUEUE),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
    @ActivationConfigProperty(propertyName = "maxSession", propertyValue = "1"),
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")})
@ResourceAdapter("activemq-rar")
public class PreloadArquivoRetornoItemSinteticoQueue implements MessageListener {

    public static final String JNDI_QUEUE = "java:/jms/preloadArquivoRetornoItemSinteticoQueue";

    @Override
        public void onMessage(final Message message) {
            final ObjectMessage objectMessage = (ObjectMessage) message;

            final ArquivoRetornoDTO arquivoRetornoDTO = (ArquivoRetornoDTO) objectMessage.getObject();
        }
}

I have already performed the configuration suggested in the documentation for "Producers", but I don’t know how to make the "Consumers" accept the classes of the Packages of my application because I don’t use the Factory Connection of activemq as it appears in the documentation, but as @Messagedrive.

For "Producers", I added the following variable in the "enviroments" section of the Docker-Compose for the activemq instance and is allowing sending messages:

JAVA_OPTS: "-Dorg.apache.activemq.SERIALIZABLE_PACKAGES=*"

What I haven’t figured out yet is how to set up the "Consumers" to receive messages. So where do I set up in the message-drive bean something like setTrustAllPackages=true ?

Ambience:

  • Wildfly 15.0.1 Final
  • Activemq 5.15.8

Resource Adapter on standalone-full.xml:

        <subsystem xmlns="urn:jboss:domain:resource-adapters:5.0">
            <resource-adapters>
                <resource-adapter id="activemq-rar">
                    <archive>activemq-rar-5.15.8.rar</archive>
                    <transaction-support>XATransaction</transaction-support>
                    <config-property name="UseInboundSession">false</config-property>
                    <config-property name="Password">activemqadmin</config-property>
                    <config-property name="UserName">activemqadmin</config-property>
                    <config-property name="ServerUrl">tcp://activemq:61616</config-property>
                    <connection-definitions>
                        <connection-definition class-name="org.apache.activemq.ra.ActiveMQManagedConnectionFactory" jndi-name="java:/jms/odinConnectionFactory" enabled="true" pool-name="ConnectionFactory">
                            <xa-pool>
                                <min-pool-size>1</min-pool-size>
                                <max-pool-size>20</max-pool-size>
                                <prefill>false</prefill>
                                <is-same-rm-override>false</is-same-rm-override>
                            </xa-pool>
                        </connection-definition>
                    </connection-definitions>
                    <admin-objects>
                        <admin-object class-name="org.apache.activemq.command.ActiveMQQueue" jndi-name="java:/jms/preloadArquivoRetornoItemSinteticoQueue" use-java-context="true" pool-name="PreloadArquivoRetornoItemSinteticoQueue">
                            <config-property name="PhysicalName">jms/preloadArquivoRetornoItemSinteticoQueue</config-property>
                        </admin-object>
                    </admin-objects>
                </resource-adapter>
            </resource-adapters>
        </subsystem>

Exception:

java.lang.ClassNotFoundException: Forbidden class
foo.bar.ArquivoRetornoDTO! 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.    at
org.apache.activemq.util.ClassLoadingAwareObjectInputStream.checkSecurity(ClassLoadingAwareObjectInputStream.java:112)    at
org.apache.activemq.util.ClassLoadingAwareObjectInputStream.resolveClass(ClassLoadingAwareObjectInputStream.java:57)    at
java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1868)    at
java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1751)    at
java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2042)    at
java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1573)    at
java.io.ObjectInputStream.readObject(ObjectInputStream.java:431)    at
org.apache.activemq.command.ActiveMQObjectMessage.getObject(ActiveMQObjectMessage.java:211)
[wrapped] javax.jms.JMSException: Failed to build body from content. 
Serializable class not available to broker. Reason:
java.lang.ClassNotFoundException: Forbidden class
foo.bar.ArquivoRetornoDTO! 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.    at 
org.apache.activemq.util.JMSExceptionSupport.create(JMSExceptionSupport.java:36)    at 
org.apache.activemq.command.ActiveMQObjectMessage.getObject(ActiveMQObjectMessage.java:213)    at
foo.bar.PreloadArquivoRetornoItemSinteticoQueue.onMessage(PreloadArquivoRetornoItemSinteticoQueue.java:61)    at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)…

1 answer

0

After two days of cracking my head, I solved the problem by setting the system properties below in a Singleton:

@Singleton
@Startup
public class InicializadorCargaDadosIniciais implements Serializable {

@PostConstruct
    public void create() {

        System.setProperty("org.apache.activemq.SERIALIZABLE_PACKAGES", "*");
        System.setProperty("org.apache.activemq.artemis.jms.deserialization.whitelist", "*");
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.