//JBoss eap 6.4 integration with ActiveMQ and consuming messages remotely.
//Java class to send messages to ActiveMQ
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class Publisher
{
private Connection connection;
private Session session;
private Queue queue;
private MessageProducer sender;
private ActiveMQConnectionFactory connectionFactory;
private String brokerUrl;
private String username;
private String password;
private String queueName;
public Publisher(){
this.brokerUrl = "tcp://192.168.56.102:61616";
this.username = "jbossamq";
this.password = "jbossamq123";
this.queueName = "HelloworldQ";
connectionFactory = new ActiveMQConnectionFactory(this.username, this.password, this.brokerUrl);
}
public void initQueueConnection() throws Exception {
connection = connectionFactory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
queue = session.createQueue(queueName);
sender = session.createProducer(queue);
}
public void sendMsgToQueue(String serviceInput) throws Exception{
initQueueConnection();
TextMessage requestTextMessage = session.createTextMessage(serviceInput);
sender.send(requestTextMessage);
sender.close();
session.close();
connection.close();
}
public static void main(String[] args) {
Publisher pub = new Publisher();
try {
pub.sendMsgToQueue("helloworld");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
pom.xml
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
<version>5.15.15</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
<build>
<finalName>publisher</finalName>
</build>
//JBoss eap 6.4 integration with ActiveMQ and consuming messages remotely.
package org.jboss.as.quickstarts.mdb;
import java.util.logging.Logger;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.jboss.ejb3.annotation.ResourceAdapter;
@MessageDriven(name = "HelloworldQ", activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "HelloworldQ"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
@ResourceAdapter(value="activemq-ra.rar")
public class HelloWorldQueueMDB implements MessageListener {
private final static Logger LOGGER = Logger.getLogger(HelloWorldQueueMDB.class.toString());
/**
* @see MessageListener#onMessage(Message)
*/
public void onMessage(Message rcvMessage) {
TextMessage msg = null;
try {
if (rcvMessage instanceof TextMessage) {
msg = (TextMessage) rcvMessage;
LOGGER.info("Received Message from queue: " + msg.getText());
} else {
LOGGER.warning("Message of wrong type: " + rcvMessage.getClass().getName());
}
} catch (JMSException e) {
throw new RuntimeException(e);
}
}
}