Показ дописів із міткою spring. Показати всі дописи
Показ дописів із міткою spring. Показати всі дописи

субота, 3 січня 2015 р.

JMS (from my old report)

You can see original slides in http://www.slideshare.net/mybbslides/jms-14767911


General concepts


Queues


Topics


Message types

JMS specification provides standard implementations of javax.jms.Message:
1. javax.jms.BytesMessage - array of bytes
2. javax.jms.MapMessage - key-value pairs
3. javax.jms.ObjectMessage - serialized Java object
4. javax.jms.StreamMessage - stream of Java primitive values
5. javax.jms.TextMessage - Java String object (used for XML)

JMS working algorithm

1. Obtain a javax.jms.ConnectionFactory using JNDI lookup. (ConnectionFactory
names are different depending on JMS provider.)
2. Obtain a javax.jms.Connection from the ConnectionFactory.
3. Obtain a javax.jms.Session from the Connection.
4. Create a javax.jms.MessageProducer or javax.jms.MessageConsumer from the Session.
5. Send a message for the MessageProducer, or receive a message for the MessageConsumer
(synchronous) or set a message listener (asynchronous).
6. Start the Connection to start message delivery.
7. Finally close the Connection.

Synchronous messaging

while(true) {
    Message message = consumer.receive();
    ....
}

Asynchronous messaging (listener class)

class AsyncConsumerMessageListener implements MessageListener {
    public void onMessage(Message message) {
        TextMessage msg = (TextMessage) message;
        ....
    }
}

Asynchronous messaging (listener class usage)

AsyncConsumerMessageListener asyncConsumer = new AsyncConsumerMessageListener();
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(asyncConsumer);

Spring amqConnectionFactory

<bean class="org.apache.activemq.ActiveMQConnectionFactory" id="amqConnectionFactory">
    <property name="brokerURL" value="${jms.url}"/>
</bean>


1) jms.url=tcp://localhost:61616
2) jms.url=vm://localhost

Spring jmsQueueConnectionFactory

<bean id="jmsQueueConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
    <property name="targetConnectionFactory" ref="amqConnectionFactory"/>
</bean>

Spring listener-container

<jms:listener-container connection-factory="jmsQueueConnectionFactory" destination-type="queue" container-type="default">
    <jms:listener destination="TESTQUEUE" method="onMessage" ref="webConsumer"/>
</jms:listener-container>

Spring jmsTemplate

<bean class="org.springframework.jms.core.JmsTemplate" id="jmsTemplate">
    <constructor-arg ref="jmsQueueConnectionFactory"/>
</bean>

Spring message listener

@Component
public class WebConsumer implements MessageListener
{
    public void onMessage( final Message message ) {...}
}

Spring producer

@Component
public class WebProducer
{
    @Autowired
    private JmsTemplate jmsTemplate;

    public void send(String message) throws JMSException
    {
        jmsTemplate.convertAndSend("TESTQUEUE", message);
    }
}

Sources and useful links

1. Official JMS tutorial book
2. http://www.thirdeyeconsulting.com/indyjug/jms/jms.html
3. http://www2.sys-con.com/itsg/virtualcd/Java/archives/0604/chappell/index.html

четвер, 6 листопада 2014 р.

Design patterns in Spring framework

 1. Creational Patterns

Singleton – this is the most common and simple design pattern. Pattern consists  with one class that is responsible for creation one instance of itself. Beans are defined in spring config xml files. Method getBean(beanName) returns one instance of predefined bean object only. Spring framework implements main idea of this pattern only. In singleton pattern class must control that only one instance of it will be created unlike in spring framework it implemented as part of bean scoping. By default all beans in spring are singletons. This can be changed by using scope attribute.

Example:
<bean class="com.blogspot.mvnblogbuild.SingletonBeanExample" id="singletonBeanExample" scope="singleton"></bean>

Prototype – duplicate is created of current object in this pattern. In spring framework this pattern implements as part of bean scoping like singleton pattern.

Example:
<bean class="com.blogspot.mvnblogbuild.PrototypeBeanExample" id="prototypeBeanExample" scope="prototype"></bean>

Factory – this pattern creates set of objects with a common parent class or interface without sharing creation logic  to the client code. Spring uses factory pattern to create objects of beans using BeanFactory child classes.



XmlWebApplicationContext and ClassPathXmlApplicationContext classes are implement ApplicationContext which is extends BeanFactory. Both those classes are factories that return user defined beans.


Builder - builder design pattern is allow to create instances of comlex class by specifying interface for creating parts of product object. Spring provides programmatic means of constructing BeanDefinitions using the builder pattern through Class "BeanDefinitionBuilder".



2. Behavioural Patterns

Template method – is used extensively to deal with boilerplate repeated code. For example JdbcTemplate, JmsTemplate, JpaTemplate.

This pattern is about defining the skeleton of the algorithm in which some of the steps of the algorithm are deferred to the sub-classes or to the data member objects.

Mediator – to reduce communications between many objects this pattern provide one class which handle all this communications. Spring framework provide DispatcherServlet class.

Strategy - in this pattern behavior of program can be changed in runtime by choosing one of the algoritms




Observer - this pattern is used when you need to notify set of objects in one to many relationship. Where observer objects waiting for events from observable object.


3. Structural Patterns

Proxy – This pattern is useful to control the access to an object and expose the same interface as that of that object. Spring framework implements this pattern in its AOP support.

4. J2EE Patterns

Model View Controller . The advantage with Spring MVC is that your controllers are POJOs as opposed to being servlets. This makes for easier testing of controllers. One thing to note is that the controller is only required to return a logical view name, and the view selection is left to a separate ViewResolver. This makes it easier to reuse controllers for different view technologies.

Front Controller. Spring framework implements this pattern by DispatcherServlet to ensure an incoming request gets dispatched to your controllers.

View Helper
- Spring has a number of custom JSP tags, and velocity macros, to assist in separating code from presentation in views.