not able to Log the message in activemq using log4j2 - java

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Producer {
private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
private static String QUEUE_NAME = "kesaven";
private static final Logger logger = LogManager.getLogger(Producer.class.getName());
public static void main(String[] args) throws JMSException
{
//System.out.println(url);
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(QUEUE_NAME);
MessageProducer producer = session.createProducer(destination);
//TextMessage message = session.createTextMessage("My first log message to queue");
logger.info("My first log message to queue");
//producer.send(message);
//System.out.println("Sentage '" + message.getText() + "'");
connection.close();
}
}
i am trying to log the message to a queue using log4j2 and activemq. i am using log4j2.3 and for queue i am using activemq 5.9.0 . i am able to see exception message in queue not the message i sent. Following is the error message in queue
javax.jms.JMSException: Failed to build body from content.
Serializable class not available to broker. Reason:
java.lang.ClassNotFoundException:
org.apache.logging.log4j.core.impl.Log4jLogEvent$LogEventProxy

What appender are you using to write the data to ActiveMQ? I would suggest that instead of using Java serialization to write the log event that you serialize it to JSON, use the RFC5424 layout or some other format that doesn't require the Log4j2 jars when reading the events.

Since the Log4event is put on the queue , can you check if you have the log4j2 jars available on the broker end.

I added the following jars from log4j2 to the activeMQ server /lib directory to make this work
log4j-api-2.6.1,
log4j-core-2.6.1
I logged Strings only, if I tried to log ObjectMessages, the queue entry would not open in the Queue manager view, the JSP failed.

Related

Error while trying to send message to WMQ through java

I am trying to connect to a QM and send message to one of the queues, using below code:
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.JMSException;
import com.ibm.msg.client.commonservices.*;
import com.ibm.mq.commonservices.internal.trace.*;
import com.ibm.msg.client.jms.JmsConnectionFactory;
import com.ibm.msg.client.jms.JmsFactoryFactory;
import com.ibm.msg.client.wmq.WMQConstants;
public class MQConnect {
public static void main(String[] args) {
Connection connection = null;
Session session = null;
Destination destination = null;
Destination tempDestination = null;
MessageProducer producer = null;
MessageConsumer consumer = null;
try {
System.out.println(WMQConstants.WMQ_PROVIDER);
JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
JmsConnectionFactory cf = ff.createConnectionFactory();
cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, "hostname");
cf.setIntProperty(WMQConstants.WMQ_PORT, port);
cf.setStringProperty(WMQConstants.WMQ_CHANNEL, "channel");
cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, "QMName");
connection = cf.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("test_java_q");
producer = session.createProducer(destination);
long uniqueNumber = System.currentTimeMillis() % 1000;
TextMessage message = session.createTextMessage(" " + uniqueNumber);
connection.start();
producer.send(message);
}catch (JMSException jmsex) {
System.out.println(jmsex.getErrorCode());
System.out.println(jmsex.getLinkedException().getCause());
System.out.println(jmsex.getMessage());
}
}
}
The error I'm getting is :
Exception in thread "main" java.lang.NoClassDefFoundError:
com/ibm/msg/client/commonservices/trace/Trace
at com.ibm.msg.client.jms.JmsFactoryFactory.<clinit>(JmsFactoryFactory.java:54)
at MQConnect.main(MQConnect.java:33)
Caused by: java.lang.ClassNotFoundException: com.ibm.msg.client.commonservices.trace.Trace
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
I have the required jar files but I believe something is still missing from project references.
I have checked the jars and class specified in error is present in one of the jar files.
These are the jar files I have referenced:
com.ibm.mq.headers.jar
com.ibm.mq.jar
com.ibm.mq.pcf.jar
com.ibm.mq.tools.ras.jar
jms.jar
com.ibm.mqjms.jar
com.ibm.mq.jmqi.jar
com.ibm.mq.commonservices.jar
There are also several other jars which are not com.ibm.*
Any suggestions?
You really shouldn't be picking and choosing MQ JAR files. For IBM MQ v8.0 or higher, you should be using the com.ibm.mq.allclient.jar and jms.jar files. That's it, just 2 files. IBM has posted the com.ibm.mq.allclient.jar file on Maven (and use the most recent one). You can find com.ibm.mq.allclient.jar here and jms.jar from here.
If you are using IBM MQ v7.5 or lower (which all releases are out of support) then the correct JAR files are:
connector.jar
com.ibm.mq.jar
com.ibm.mq.commonservices.jar
com.ibm.mq.headers.jar
com.ibm.mq.jmqi.jar
com.ibm.mq.pcf.jar
com.ibm.mqjms.jar
jms.jar
fscontext.jar
jndi.jar
jta.jar
ldap.jar
Also, do NOT mix and match JAR files between different releases of IBM MQ. It will cause you all kinds of headaches.
Finally, what is this line:
destination = session.createQueue("test_java_q");
It should be:
destination = context.createQueue("queue:///MY.TEST.QUEUE");
where 'MY.TEST.QUEUE' actually exists in the queue manager. All MQ objects are case sensitive and MQ Best Practises says to use uppercase object names. i.e. avoid lowercase names.
Updated Feb. 14, 2022.
You need to add some code to your program so we can see what is going on.
(1) Add the following MQLevel class (taken from my open source project Universal File Mover) to your project:
public class MQLevel extends MQJavaLevel
{
/**
* The constructor
*/
public MQLevel()
{
super();
}
/**
* Get the IBM name for the MQ JAR class.
* #return
*/
public String getName()
{
return queryValue(0);
}
/**
* Get the version of the MQ JAR class.
* #return
*/
public String getVersion()
{
return queryValue(1);
}
}
(2) Next add the following lines of code in your main method or at the very top of your code:
System.out.println(System.getProperty("java.class.path").replace(';','\n'));
try
{
MQLevel mql = new MQLevel();
System.out.println("MQ JAR Version = " + mql.getName()+" V"+mql.getVersion());
}
catch(Exception e)
{
e.printStackTrace();
}
Note: If you are on Linux/Unix rather than Windows then change the ';' (semi-colon) in the replace method to a ':' (colon).
i.e.
System.out.println(System.getProperty("java.class.path").replace(':','\n'));
Run your program with the updates, then update the original posting both with the new output.

MessageDrivenBean isnt handle messages [Wildfly]

Im trying to develop a "Message Driven Bean" to handle all the local ActiveMQ messages, but it's the first time that i try to do something like this.
The most part of the material that i found explain how to write a MDB using JBOSS server, in this case there's a xml file with some queue information, but in all wildfly tutorials there's no mention to any kind of configuration like that.
I have the following scenario:
A simple java project like message producer
An ActiveMQ instance running local
An EJB project deployed into Wildfly 10
My producer project is able to send messages to ActiveMQ queue, this part its working,but my EJB project just have a single class called TestMDBHandle with #MessageDriven annotation. Is this enough to receive my queue messages? Because the MDB isnt working, i imagine must be a kind of configuration or property in EJB to specify the host of the message-broker.
My message producer:
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class MessageSender {
public static void main(String args[]) throws NamingException, JMSException {
MessageSender sender = new MessageSender();
sender.sender();
}
public void sender() throws NamingException, JMSException {
InitialContext jndi = null;
Session session = null;
Connection connection = null;
try {
jndi = new InitialContext();
ConnectionFactory factory = (ConnectionFactory)jndi.lookup("connectionFactory");
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = (Destination)jndi.lookup("MyQueue");
MessageProducer producer = session.createProducer(destination);
TextMessage mensagem = session.createTextMessage("Eu enviei uma mensagem!");
producer.send(mensagem);
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
connection.close();
jndi.close();
}
}
}
My jms properties located inside my producer project
java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory
java.naming.provider.url=tcp://localhost:61616
connectionFactoryNames = connectionFactory, queueConnectionFactory, topicConnectionFactory
queue.MyQueue=jms/myqueue
Finally, my ejb project have this single class, without any kind of property file or xml.
package br.com.jms.mdb;
import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
#MessageDriven(name = "meuHandler", activationConfig = {
#ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
#ActivationConfigProperty(propertyName = "destination", propertyValue = "jms/myqueue") })
public class Teste implements MessageListener {
#Resource
private MessageDrivenContext mdctx;
public Teste() {
}
#Override
public void onMessage(Message message) {
TextMessage objectMessage = null;
try {
objectMessage = (TextMessage)message;
System.out.println("Achei a mensagem : " + objectMessage.getText().toString());
}catch(JMSException e) {
e.printStackTrace();
}
}
}
Maybe you can provide a little more information such as the xml file with the queue information and the annotation properties of the MDB? Because it sounds you are heading in the right direction. The two main things:
You have to specify the exact queue that the MDB is listening to, for example through the properties of the #MessageDriven annotation (such as "name", "mappedName", "activationConfig"). And of course override the onMessage() method to process the messages.
You also have to make sure that this specific queue is available as a resource for your application. You have to provide jms configuration for this, which also defines the resource type (Queue or Topic). From your question I can't tell which of these steps you have (partly) completed.

Qpid and JNDI for encrypted messages

I'm currently working on a JMS project and I have created 2 keys and 2 certificates as well as a TrustStorage, mytruststore which I created through Qpid's UI.
In my jndi.properties file I have the following code:
//Set the InitialContextFactory class to use
java.naming.factory.initial = org.apache.qpid.jms.jndi.JmsInitialContextFactory
//Define the required ConnectionFactory instances
//connectionfactory.<JNDI-lookup-name> = <URI>
connectionfactory.myFactoryLookup = amqp://localhost:5672
connectionfactory.producerConnectionFactory = amqp://admin:admin#?brokerlist='tcp://localhost:5672?encryption_remote_trust_store='$certificates%255c/mytruststore''
connectionfactory.consumer1ConnectionFactory = amqp://admin:admin#?brokerlist='tcp://localhost:5672?encryption_key_store='C:\OpenSSL-Win64\bin\mytruststorage.jks'&encryption_key_store_password='thanos''
connectionfactory.consumer2ConnectionFactory = amqp://admin:admin#?brokerlist='tcp://localhost:5672?encryption_key_store='C:\OpenSSL-Win64\bin\mytruststorage.jks'&encryption_key_store_password='thanos''
//Configure the necessary Queue and Topic objects
//queue.<JNDI-lookup-name> = <queue-name>
//topic.<JNDI-lookup-name> = <topic-name>
queue.myQueueLookup = queue
topic.myTopicLookup = topic
queue.myTestQueue = queue
In my EncryptionExample.java class I have the following code:
package org.apache.qpid.jms.example;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class EncryptionExample {
public EncryptionExample() {
}
public static void main(String[] args) throws Exception {
EncryptionExample encryptionExampleApp = new EncryptionExample();
encryptionExampleApp.runProducerExample();
encryptionExampleApp.runReceiverExample();
}
private void runProducerExample() throws Exception
{
Connection connection = createConnection("producerConnectionFactory");
try {
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
Destination destination = createDesination("myTestQueue");
MessageProducer messageProducer = session.createProducer(destination);
TextMessage message = session.createTextMessage("Hello world!");
// ============== Enable encryption for this message ==============
message.setBooleanProperty("x-qpid-encrypt", true);
// ============== Configure recipients for encryption ==============
message.setStringProperty("x-qpid-encrypt-recipients", "CN=client1, OU=Qpid, O=Apache, C=US");
messageProducer.send(message);
session.commit();
}
finally {
connection.close();
}
}
private void runReceiverExample() throws Exception
{
Connection connection = createConnection("consumer1ConnectionFactory");
try {
connection.start();
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
Destination destination = createDesination("myTestQueue");
MessageConsumer messageConsumer = session.createConsumer(destination);
Message message = messageConsumer.receive();
if (message instanceof TextMessage) {
// application logic
System.out.println(((TextMessage) message).getText());
} else if (message instanceof BytesMessage) {
// handle potential decryption failure
System.out.println("Potential decryption problem. Application not in list of intended recipients?");
}
session.commit();
}
finally {
connection.close();
}
}
///////////////////////////////////////
// The following is boilerplate code //
///////////////////////////////////////
private Connection createConnection(final String connectionFactoryName) throws JMSException, IOException, NamingException
{
try (InputStream resourceAsStream = getResourceAsStream("jndi.properties")) {
Properties properties = new Properties();
properties.load(resourceAsStream);
Context context = new InitialContext(properties);
ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup(connectionFactoryName);
final Connection connection = connectionFactory.createConnection();
context.close();
return connection;
}
}
private InputStream getResourceAsStream(String string) {
// TODO Auto-generated method stub
return null;
}
private Destination createDesination(String desinationJndiName) throws IOException, NamingException
{
try (InputStream resourceAsStream = this.getClass().getResourceAsStream("example.properties")) {
Properties properties = new Properties();
properties.load(resourceAsStream);
Context context = new InitialContext(properties);
Destination destination = (Destination) context.lookup(desinationJndiName);
context.close();
return destination;
}
}
}
When I'm trying to build it I get the following exceptions.
Exception in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load0(Unknown Source)
at java.util.Properties.load(Unknown Source)
at
org.apache.qpid.jms.example.EncryptionExample.createConnection(EncryptionExample.java:106)
at
org.apache.qpid.jms.example.EncryptionExample.runProducerExample(EncryptionExample.java:54)
at org.apache.qpid.jms.example.EncryptionExample.main(EncryptionExample.java:48)
I assume that something's wrong with the following code in jndi.properties file:
connectionfactory.myFactoryLookup = amqp://localhost:5672
connectionfactory.producerConnectionFactory = amqp://admin:admin#?brokerlist='tcp://localhost:5672?encryption_remote_trust_store='$certificates%255c/mytruststore''
connectionfactory.consumer1ConnectionFactory = amqp://admin:admin#?brokerlist='tcp://localhost:5672?encryption_key_store='C:\OpenSSL-Win64\bin\mytruststorage.jks'&encryption_key_store_password='thanos''
connectionfactory.consumer2ConnectionFactory = amqp://admin:admin#?brokerlist='tcp://localhost:5672?encryption_key_store='C:\OpenSSL-Win64\bin\mytruststorage.jks'&encryption_key_store_password='thanos''
This is my solution Explorer:
This first and biggest problem you have is that you are trying to use connection URIs and client features from a client other than the one you have configured you project to use. You seem to be using Qpid JMS which is the new AMQP 1.0 client developed at the Qpid project. This client uses a different URI syntax that the previous AMQP 0.x clients and you will get exception from the connection factory when passing in these invalid URIs.
The other problem you will have (which was called out in the comments on your post) is that there is not message encryption feature in the AMQP 1.0 JMS client so that would be your next problem once you got the URIs correctly defined.
The documentation for the newer AMQP 1.0 JMS client is here.
You cannot use // for comments in Properties files. Use # or ! instead.
See: https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html#load-java.io.Reader-

How to publish messages to EMS Topic using Java

I would like to publish a test message to EMS topic and could use some directions. So far I have managed to do this
import com.tibco.tibjms.TibjmsConnectionFactory;
import com.tibco.tibjms.TibjmsTopicConnectionFactory;
public class Connect {
public static void main(String[] args) {
String url = "tcp://host:6600";
TibjmsConnectionFactory cf = new TibjmsTopicConnectionFactory(url);
cf.setUserName("user1");
cf.setUserPassword("");
System.out.println(cf);
}
}
which produces the below. How do I publish a message to topic "topic1" or queue "Q1"
TopicConnectionFactory[URL=tcp://localhost:6600;clientID=null;Properties={com.tibco.tibjms.factory.password=, com.tibco.tibjms.factory.username=user1}]
I created the following code by modifying the "tibjmsMsgProducer.java" from my EMS 8.0 "sample" folder. Look at all the Java example in this folder for further references.
This code publish a simple hard-coded text message to a local EMS with default user and password. The target Topic is "topic1" (on the last line).
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
public class tibjmsMsgTopicProducer {
static String serverUrl = "localhost";
static String userName = "admin";
static String password = "admin";
public static void sendTopicMessage(String topicName, String messageStr) {
Connection connection = null;
Session session = null;
MessageProducer msgProducer = null;
Destination destination = null;
try {
TextMessage msg;
System.out.println("Publishing to destination '" + topicName
+ "'\n");
ConnectionFactory factory = new com.tibco.tibjms.TibjmsConnectionFactory(
serverUrl);
connection = factory.createConnection(userName, password);
/* create the session */
session = connection
.createSession(javax.jms.Session.AUTO_ACKNOWLEDGE);
/* create the destination */
destination = session.createTopic(topicName);
/* create the producer */
msgProducer = session.createProducer(null);
/* publish messages */
/* create text message */
msg = session.createTextMessage();
/* set message text */
msg.setText(messageStr);
/* publish message */
msgProducer.send(destination, msg);
System.out.println("Published message: " + messageStr);
/* close the connection */
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
/*-----------------------------------------------------------------------
* main
*----------------------------------------------------------------------*/
public static void main(String[] args) {
tibjmsMsgTopicProducer.sendTopicMessage("topic1",
"This is the message content !");
}
}
Note : You could also want to use EMS with Spring-JMS for a more "Enterprise grade" solution. The code above is a lot simpler.
Note2: I made the method "static". This is only for demonstration purpose. Connections are costly in JMS, so normally we try to reuse them. See all TIBCO provided example for better setup of the Java classes. Instantiate and reuse connections if you can.
Additionally, J2EE or Spring solutions will have support for connection pools built-in.
I haven't touched EMS for some time - but basically EMS is nothing but a JMS implementation. All implementation specific stuff has been hidden for you. You just use standard JMS way to pub/sub to topics, which you can find good example on Java tutorial and online sources. I would save my ugly sample code here :-)
You can take a look at this test project #gelnyang built. And this is the class for publishing EMS message specifically. Under the project, you can find other EMS related functionalities as well.

Add manually mappedName in Message Driven Bean at runtime

I need to make simple Message Driven Bean that will listen on dynamically added queue locataions. I have tried few ways to implement this, but none of them worked. I have appplication that uses esb and java message queues, and I'm trying to read queue location from config file, during the runtime, and thus tell my message driven bean what is the queue on which to listen. I am not either sure that this is possible.
I also tried to implement message listener, but because I have to use ejb module, and ejb module does not support main method, it requires his own container (like message driven bean), I don't know what to use instead of main method to achive the same goal. I am not able to use session beans because I need to achieve asynchronous communication between client and service.
I also tried to use client application (although it is not one of the options), but maven project does not support debug and run functions for this type of application in netbeans.
Does anyone know any solution for this problem, or at least have some idea?
This may not be the best solution, but it is possible to receive and process JMS messages asynchronously with a Stateful Session Bean doing something like this:
package com.example.statefuljms;
import javax.annotation.Resource;
import javax.ejb.Local;
import javax.ejb.Stateful;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
#Stateful
#Local(MessageReceiverLocal.class)
public class MessageReceiver implements MessageReceiverLocal, MessageListener {
#Resource(mappedName = "ConnectionFactory")
private ConnectionFactory connectionFactory;
private QueueConnection connection;
#Override
public void start(String queueName) throws JMSException, NamingException {
Context initialContext = new InitialContext();
connection = (QueueConnection) connectionFactory.createConnection();
QueueSession session = (QueueSession) connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue) initialContext.lookup(queueName);
QueueReceiver receiver = session.createReceiver(queue);
receiver.setMessageListener(this);
connection.start();
}
#Remove
#Override
public void stop() throws JMSException {
connection.stop();
connection.close();
}
#Override
public void onMessage(Message message) {
// handle message here
}
}
Use a Singleton to test:
package com.example.statefuljms;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.jms.JMSException;
import javax.naming.NamingException;
#Startup
#Singleton
public class Test {
#EJB
private MessageReceiverLocal messageReceiver;
#PostConstruct
public void run() {
messageReceiver.start("/queue/myQueue");
}
#PreDestroy
public void cleanup() {
messageReceiver.stop();
}
}

Categories

Resources