How to integrate Java Application with Amazon MQ
Posted By : Vikash Patwal | 17-Aug-2018
Introduction->
Amazon MQ is another administration that was propelled amid AWS
Amazon MQ Broker->
We can make
Why ActiveMQ, Why not database to store messages?
Truly, we can utilize the database to store messages and process it. Yet, at whatever point correspondence occurs between two applications, once the message is gotten it ought to be prepared and erased i.e. for each message section there ought to be embed and erase task. This will work easily for
Integration->
Producer Class->
import org.apache.activemq.jms.pool.PooledConnectionFactory;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class Producer {
public static void main(String[] args) throws JMSException {
ActiveMQConnectionFactory connFactory = new ActiveMQConnectionFactory("AMQP_URI");
// Specify the master username and password.
connFactory.setUserName("username");
connFactory.setPassword("password");
PooledConnectionFactory pooledConnFactory = new PooledConnectionFactory();
pooledConnFactory.setConnectionFactory(connFactory);
pooledConnFactory.setMaxConnections(10);
Connection producerConnection = pooledConnFactory.createConnection();
producerConnection.start();
Session producerSession =
producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination producerDest = producerSession.createQueue("MesgQueue");
MessageProducer producer = producerSession.createProducer(producerDest);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
String text = "Hi message for produce";
TextMessage producerMessage = producerSession.createTextMessage(text);
producer.send(producerMessage);
System.out.println("Message sent successfully");
producer.close();
producerSession.close();
producerConnection.close();
}
}
Consumer Class->
import org.apache.activemq.jms.pool.PooledConnectionFactory;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class Consumer {
public static void main(String[] args) throws JMSException {
ActiveMQConnectionFactory connFactory = new ActiveMQConnectionFactory("link");
// Specify the master username and password.
connFactory.setUserName("username");
connFactory.setPassword("password");
Connection consumerConn = connFactory.createConnection();
consumerConn.start();
Session consumerSession = consumerConn.createSession(false,Session.AUTO_ACKNOWLEDGE);
Destination consumerDest = consumerSession.createQueue("MesgQueue");
MessageConsumer consumer = consumerSession.createConsumer(consumerDest);
Message consumerMesg = consumer.receive(1000);
TextMessage consumerTextMesg = (TextMessage) consumerMesg;
System.out.println("Received Message is : " + consumerTextMesg.getText());
consumer.close();
consumerSession.close();
consumerConn.close();
}
}
Dependency->
Convert Java application into Maven project. Update pom.xml file with the following code.
org.apache.activemq activemq-client 5.15.0 org.apache.activemq activemq-pool 5.15.0
Run Producer class to communicate something specific and Run Consumer class to get the message sent by the Producer.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Vikash Patwal
Vikash Patwal is Masters in Computer Applications and good in Java , he is hardworking team player.