Failed to build body from content in activeMQ - Spring generic message used - java

I send a org.springframework.messaging.support.GenericMessage to a queue in ActiveMQ by org.springframework.jms.core.JmsTemplate. in ActiveMQ, I see this message :
Cannot display ObjectMessage body. Reason: Failed to build body from content.
Serializable class not available to broker.
Reason: java.lang.ClassNotFoundException: org.springframework.messaging.support.GenericMessage
and so I can't read that message in a client.
I set trustAllPackages to true in my activeMQConnectionFactory and problem doesn't solve. How to solve it ?

The JmsTemplate will serialize the entire GenericMessage as a java object, so spring-messaging is needed on the class path of the receiving system as well.
class not available to broker.
It looks like you might be trying to view the message in the admin UI, which doesn't understand spring-messaging classes.
If you want to map the GenericMessage to a JMS Message instead, use the JmsMessagingTemplate instead (one of the send() methods); the broker might be able to display such a message (depending on the payload type).

Simply put:
Add the required jar or class files or the serialised messages in the lib folder of activemq and restart activemq.
It worked for me

Related

Configure ACTIVEMQ_OPTS in ActiveMQ 5.15.4

I was trying to send an ObjectMessage containing a myObject, instance of myClass, using ActiveMQ as JMS Provider. Call to ActiveMQ is made through Mule 4.3.0.
An error occurred in the receiving application:
Failed to build body from content. Serializable class not available to broker. Reason: java.lang.ClassNotFoundException: Forbidden class client.MyClass! 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.
the page says:
If you need to exchange object messages, you need to add packages your applications are using. You can do that with by using
org.apache.activemq.SERIALIZABLE_PACKAGES system property of the
broker. You can add this system property to ACTIVEMQ_OPTS variable in
${ACTIVEMQ_HOME}/bin/env script.
I am using ActiveMQ 5.15.4 . In the \bin subfolder, there is no env script, only the activemq script.
Where should I configure ACTIVEMQ_OPTS so that I, will not get forbidden class error. Or if any alternative solutions will help.
You should define ACTIVEMQ_OPTS in your operating system shell, using the appropriate method for that operating system. For example in Linux set it in the user's profile script, or use the bin/env script.
If you are using Windows you can set it using the UI.

How can I abstract RabbitMQ Consumer and Producer from the main project source code

I have a project with "n" number of microservices module that uses RabbitMQ.
Currently, each microservices have their own implementation of RabbitMQ consumer and producer. But I want to abstract and isolate this interaction with RabbitMQ to a common method in an external library so my project is not tightly coupled with RabbitMQ broker - I would want to change it to an ActiveMQ, etc., in the future with relative ease.
The Producer abstraction seems straight forward but how do I achieve Consumer abstraction?
I want to be able to call the consumer method with the queue name as an argument from outside/external method and it should return me the message that it received from the given queue name.
I have looked into using SpringAmqp #RabbitListener annotation or RabbitTemplate with Message Listener configured, but it looks like I need to first configure the queue name statically. I also tried Java client Channel basicConsume but I could not figure a way out to extract/send the message to an external method using basicConsume.
Any help with a sample example would be greatly appreciated.
Consider using Spring Integration instead of using Spring AMQP directly.
That way, you can easily swap out the inbound/outbound channel adapters with different technologies because the adapters there use a common (spring-messaging) Message<?> abstraction.
but it looks like I need to first configure the queue name statically.
That is not true; the queue name(s) can be populated via properties and/or SpEL expressions.

Apache does not update documentation? Major Bug in Java

I'm getting this error
Cannot display ObjectMessage body. Reason: Failed to build the body from content. Serializable class not available to the broker. Reason: java.lang.ClassNotFoundException: Forbidden class com.company.data.TicketData! This class is not trusted to be serialized as ObjectMessage payload. Please take a look at this for more information on how to configure trusted classes.
I added System.setProperty("org.apache.activemq.SERIALIZABLE_PACKAGES","*"); in my code that invokes the creation of the JMS and doesn't work.
I also set setx org.apache.activemq.SERIALIZABLE_PACKAGES "*" in the cmd but still the same error.
Even if you check the error page.
it talks about an env script file that does not exist when you download the Apache ActiveMQ.
What can I do?? There are some files in /config but I don't see how do enable this? Why Apache has documentation that even doesn't work?
This is expected. The Java runtime of your broker can only deserialize the ObjectMessage if
The class is part of the broker's classpath. The exception
"java.lang.ClassNotFoundException" sounds like that's the issue. Try adding your company's jar file into the ActiveMQ classpath.
The class implements java.io.Serializable. Adding something to your
client's Java runtime won't help. It must be know to the broker, so
best is really to let your class implement that interface.
ActiveMQ
also requires to 'trust' an Object. So your Broker must have this class whitelisted, for example by starting the Broker with -Dorg.apache.activemq.SERIALIZABLE_PACKAGES=* - this only works onwards from a certain ActiveMQ version > 5.12.2
Quite frankly, you should stay away from JMS ObjectMessage. There was an ActiveMQ bug in the area a while ago and it's a constant security hole. Try to send as BytesMessage as externalized Object, or maybe send your Object as TextMessage in XML or JSON format.

Camel kafka logging incorrect details in messageHistory

I am using camel kafka component 2.19 (Latest). I am initializing kafka producer endpoint with "my-topic". but topic is calculated at the runtime say "my-error-topic" and I set it in the header so the message is produced to "my-error-topic". Everything works fine.
The problem is when messageHistory is logged, it logs initial route with the topic which I used for initialization which makes it misleading information for support guys because it gives them an idea that message is produced to "my-topic." As a workaround, I have stopped logging message history. But still I got it in logs from defaultErrorHandler.log(). Hence it is still misleading.
Please tell me the solution.
No this is correct as it does it, as it logs the endpoint url from the route (eg you can find exactly where in the route it was). Any kind of header override is a special use-case here, you can log the headers if you use error handling in Camel where you can log anything you want, such as the exception message, stacktrace, message body, headers etc.

Trying to capture response from ActiveMQ in my camel route

I am attempting to create a camel route using blueprint that sends a message on an activeMQ queue then listens to the response on the temporary queue created in the request. This seems pretty basic, but I can't find an example that utilizes it.
I have tried searching and reading the docs and here's what I've found:
http://camel.apache.org/jms.html
http://camel.apache.org/exchange-pattern.html
http://camel.apache.org/request-reply.html
https://access.redhat.com/documentation/en-US/Red_Hat_JBoss_Fuse/6.0/html/EIP_Transaction_Guide/files/FMRTxnJMSSynchronous.html
http://kosalads.blogspot.com/2014/04/ApacheCamelRequestReplyPatternWithJavaDS.html
http://grokbase.com/t/camel/users/128n88xeva/how-to-use-request-reply-in-jms
http://camel.465427.n5.nabble.com/ExchangePattern-InOut-I-Can-t-get-any-response-td5056301.html
https://examples.javacodegeeks.com/enterprise-java/apache-camel/apache-camel-exchange-example/
Which is frustrating.
I have my activeMQ component set up such:
<to pattern="InOut" uri="activemq:queue:tripRequest.updateStatus.v1.0?useMessageIDAsCorrelationID=true"/>
<log message="Update Status responded ${out.body}"/>
The log shows the input XML, which surprised me. After checking the docs, I created a new activeMQ instance that listens to the same queue and dumps to a log, but this threw errors and it keeps mixing up my log and unmarshal objects on my other route.
How can I accomplish this?
Check the answer in the link below. It should give you hints on how to build your active-mq uri for a request/reply scenario.
Implement Request-Reply pattern using ActiveMQ, Camel and Spring

Categories

Resources