JAVA MESSAGING SERVICE- SUBSCRIBER/CONSUMER USING ACTIVEMQ

In continuation with the previous post on Producer of Active MQ: https://lakshaysuri.wordpress.com/2016/08/15/java-messaging-active-mq/

This post will detail about how the consumer could connect to the queue on which the producer has sent the messages and retrieve them.

Step1: A very basic example of Consumer class using Spring.

 

@Component

public class Subscriber {

@Autowired

JmsTemplate jmsTemplate;

// in order to read the queue name from the URI

@Value(“${jms.queue.destination}”)

String destinationQueue;

private Subscriber() {}

public static Subscriber newInstance() {

return new Subscriber();

}

// in order to read the queue name independent of the URI

public void setReceivingQueue(String destinationQueue) {

this.destinationQueue = destinationQueue;

}

// receiving the message from the mentioned queue

public String receiveString() {

String dataReceived = (String) jmsTemplate.receiveAndConvert(destinationQueue);

return dataReceived;

}

public Object receiveObject() {

Object dataReceived = jmsTemplate.receiveAndConvert(destinationQueue);

return dataReceived;

}

}

Step2: Spring Receiver Controller Class

@RestController

public class ActiveMqReceiverController {

@Autowired

Subscriber subscriber;

@RequestMapping(value=”/receive”)

public Object receive() {

return subscriber.receiveObject();

}

}

Step3: Spring Boot Initializer

@Configuration

@EnableAutoConfiguration

@ComponentScan

public class JmsReceiverApplication extends SpringBootServletInitializer {

public static void main(String[] args) {

SpringApplication.run(JmsReceiverApplication.class, args);

}

}

Step4: application.properties file

spring.activemq.broker-url=failover://tcp://localhost:61616

server.port=9000

#user name & password in order to connect to the activemq on the port specified

spring.activemq.user=****

spring.activemq.password=****

#queue name where the message will be consumed by consumer.

jms.queue.destination=activemqdestination

Step5: Consumer Test class

@RunWith(SpringJUnit4ClassRunner.class)

@SpringBootTest(classes = JmsReceiverApplication.class)

@WebAppConfiguration

public class ConsumerTest {

private static final Logger LOGGER = Logger.getLogger(ConsumerTest.class);

@Autowired

Subscriber subscriber;

@Test

public void testReadBinaryDataFromQueue() {

String messageReceived = subscriber.receiveString();

LOGGER.info(“Message Received is” + messageReceived)

}

}

Note:

  1. Messages can be received from the queue by directly using receiveAndConvert() method of JmsTemplate class as shown in Test class above or by hitting the URL: http://localhost:9000/receive. Do ensure that Spring application is run before hitting this URL.
  2. The “receive” in the URL is mapped to the controller code in ActiveMqReceiverController class.
  3. Messages once consumed will appear in the Active MQ console as Dequeued and the remaining would appear as Pending and in Enqueued status.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s