The purpose of this text is to entertain the readers. With questions included to understand the text content.Full description
Per Unit System explaination- with solved Example
Full description
Full description
D U E T O the increasing cost of energy and the difficuhy of obtaining raw materials, economy has a high priority in all aspects of design. In the design of multistory steel buildings, sa…Full description
Exercise and answers
Vernier Caliper exercises.
Spring 4+JMS+ActiveMQ example with @JmsListener & @EnableJms In the previous post over post over Spring with JMS, we have seen how applications can communicate using JMS, leveraging Spring’s support for JMS. This post goes a step further, and shows an alternative to avax.jms.MessageListener which allows us to create MessageListeners using plain POJO’s, thanks to Spring’s very own @JmsListener , used in combination with JmsListenerConnectionFactory and @EnableJMS.. Let’s get started. @EnableJMS
Following technologies being used: Spring 4.3.0.RELEASE Spring JMS 4.3.0.RELEASE ActiveMQ 5.13.3 5.13.3 Maven 3 JDK 1.7 Tomcat 8.0.21 Eclipse MARS.1 Release 4.5.1 logback 1.1.7
Shown above is a POJO, which does not implement javax.jms.MessageListener, just annotated with @JmsListener which marks a method to be the target of a JMS message listener on the specified destination(). What we still have to do is to : 1. Configure a message-listener-container [ with JmsListenerContainerFactory] : which listens on a destination [can be the one used with @JmsListener] and when any message arrives on this destination, it retrieved that message and passes to the bean annotated with @JmsListener for that destination. 2. Use @EnableJms which enables detection of JmsListener annotations on any Spring-managed bean in the container.
Below shown is the configuration we have just discussed:
@Configuration @EnableJms public class MessagingListnerConfiguration { @Autowired ConnectionFactory connectionFactory; @Bean public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() { DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); factory.setConnectionFactory(connectionFactory); factory.setConcurrency("1‐1"); return factory; } }
Submit Your Resume - Top Companies are Hiring DefaultJmsListenerContainerFactory is a JmsListenerContainerFactory implementation to build a regular DefaultMessageListenerContainer. You can configure several properties. At the very least, it needs a connection factory. Additionally, we have specified the concurrency [max number of concurrent users/consumers] using setConcurrency(“lowwe-upper”). You can also use setConcurrency(“upper”) which means lower will be 1.
Note : You can also specify the container factory to be used in @JmsLietener annoatation, with
containerFactory attribute defining the name of the JmsListenerContainerFactory bean to use. When none is set a JmsListenerContainerFactory bean with name jmsListenerContainerFactory is assumed to be present. That’s all you need to configure the Message listeners using @JmsListener. Complete codes of both producer & consumer applications is shown below [as well as in download section].
Start the damn applications, Bill We will be using Apache Active MQ. In case you did not, download ActiveMQ Message Broker, unzip it, goto bin directory and start it using $ ./activemq start. You can quickly check the WebConsole [available at http://localhost:8161/admin/ with credentials admin/admin .
Application(s) Overview: In this example, we have two applications A & B trying to communicate with each other via sending Messages on Queues. Although we could have opted for Topic instead, Queue is perfect for this one producer-one consumer scenario. A sends a message [a pojo object] to a Queue [ order-queue] and listens for the response on another Queue [order-response-queue]. B is listening on order-queue, and upon receiving the message, B will send a reply [a pojo response object] to A on order-response-queue. A short but simple example of interapplication communication using JMS.
Below are the main’s of both Producer [A] & Consumer[B] applications.
import com.websystique.spring.model.Product; import com.websystique.spring.service.ProductService; import com.websystique.spring.util.BasicUtil; public class ProducerApplication { static final Logger LOG = LoggerFactory.getLogger(ProducerApplication. class); private static AtomicInteger id = new AtomicInteger(); public static void main(String[] args){ AbstractApplicationContext context = new AnnotationConfigApplicationContext( AppConfig.class);
public class ConsumerApplication { static final Logger LOG = LoggerFactory.getLogger(ConsumerApplication. class); public static void main(String[] args) { AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig. class); try { Thread.sleep(60000); } catch (InterruptedException e) { e.printStackTrace(); }
((AbstractApplicationContext) context).close(); }
}
Start Application A [Producer]. Goto web-console, and click on queue tab. You should see two queues being created with a message on one queue [A has just sent a message] while other empty.
Now start Application B [Consumer], again check queues on web-console, you will see message being dequeued on first queue [B has just processed it] and new message being enqueued [B has sent the response to A] and eventually dequeued [A got the response] on second queue.
Complete Code for Producer+Consumer Applications Let’s start with coding Producer Application [A]. Consumer Application [B] is exactly same as A, but listening and sending on opposite queues.
@Configuration public class MessagingConfiguration { private static final String DEFAULT_BROKER_URL = "tcp://localhost:61616"; private static final String ORDER_QUEUE = "order‐queue";
@Bean public ActiveMQConnectionFactory connectionFactory(){ ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(); connectionFactory.setBrokerURL(DEFAULT_BROKER_URL); connectionFactory.setTrustedPackages(Arrays.asList( "com.websystique.spring","java.util")); return connectionFactory; } @Bean public JmsTemplate jmsTemplate(){ JmsTemplate template = new JmsTemplate(); template.setConnectionFactory(connectionFactory()); template.setDefaultDestinationName(ORDER_QUEUE); return template; } }
@Configuration @EnableJms public class MessagingListnerConfiguration { @Autowired ConnectionFactory connectionFactory; @Bean public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
public class MessageReceiver { static final Logger LOG = LoggerFactory.getLogger(MessageReceiver. class); private static final String ORDER_RESPONSE_QUEUE = "order‐response‐queue";
@Service("productService") public class ProductServiceImpl implements ProductService{ static final Logger LOG = LoggerFactory.getLogger(ProductServiceImpl. class);
package com.websystique.spring.util; import java.util.UUID; public class BasicUtil { public static String getUniqueId(){ return UUID.randomUUID().toString(); }
@Configuration public class MessagingConfiguration { private static final String DEFAULT_BROKER_URL = "tcp://localhost:61616"; private static final String ORDER_RESPONSE_QUEUE = "order‐response‐queue";
@Bean public ActiveMQConnectionFactory connectionFactory(){ ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(); connectionFactory.setBrokerURL(DEFAULT_BROKER_URL); connectionFactory.setTrustedPackages(Arrays.asList( "com.websystique.spring","java.util")); return connectionFactory; }
@Bean public JmsTemplate jmsTemplate(){ JmsTemplate template = new JmsTemplate(); template.setConnectionFactory(connectionFactory()); template.setDefaultDestinationName(ORDER_RESPONSE_QUEUE); return template; } }
@Service("orderService") public class OrderServiceImpl implements OrderService{ static final Logger LOG = LoggerFactory.getLogger(OrderServiceImpl. class);