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 re:Invent 2017 which is an overseen message expediting administration for Apache ActiveMQ. ActiveMQ is open source benefit which gives dependable correspondence between at least two procedures and can be utilized for correspondence inside huge basic applications. This backings different programming dialects, working frameworks and informing conventions by taking out the requirement for overseeing and keeping up our own informing framework. It additionally gives guide support access to ActiveMQ.

 

Amazon MQ Broker->

We can make message representative as per our necessities. A dealer can be a solitary occurrence intermediary or dynamic/reserve specialist. Single Instance Broker contains one intermediary in one accessibility It speaks with the application and AWS stockpiling area. It can be utilized for advancement and testing. Dynamic/Standby Broker contains two merchants in two distinctive accessibility zones for high accessibility. It will speak with the application and shared stockpiling area. It can be utilized for programmed failover. For the two modes, Amazon MQ gives information replication crosswise over AZs.

 

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 few messages without influencing the execution of the database. In the event that we need to deal with a huge number of messages between these applications, at that point it might influence database execution and it might have a tendency to come up short. Amazon MQ goes about as message merchant benefit for Apache ActiveMQ which can be utilized to deal with this utilization case. This will deal with support of ActiveMQ representatives and perform improvements to keep away from overhead. It gives a component of driving messages to customers rather than purchaser surveying for another message. Along these lines, this diminishes the inertness engaged with preparing new messages.

 

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.

About Author

Author Image
Vikash Patwal

Vikash Patwal is Masters in Computer Applications and good in Java , he is hardworking team player.

Request for Proposal

Name is required

Comment is required

Sending message..