Wednesday, December 5, 2012

Example of Spring, ActiveMQ to send Email





Hi,


I decided to write this blog because I could not find one simple example in combination to Spring, ActiveMQ and Email the message from queue.


Problem taken

What I am going to do in this example is:
  1. Create one Spring application, that has one POJO i.e. Entity, I took Mobile as my pojo with three attributes as Company, Model and Price.
  2. There will be one message producer which will post one message (as entity Mobile) to queue-one.
  3. There will be a listener to queue-one and on receiving a message it will de-queue the message.  Further after processing Listener post message to queue-two.
  4. This queue-two has another listener which de-queue the message from queue-two and process it to send a mail. 


Pictorial Presentation




Technologies Used

Below is the stack of technologies

  • Spring 3
  • ActiveMQ 5.6.0
  • Java Mail
  • Tomcat 7
  • Maven


How to execute and flow explanation
  1. You need to hit url http://host:port/app/main/amqmobile/form.html
  2. Based on given configuration this url will search for file form.jsp inside "WEB-INF/amqmobile" folder in your web application.
  3. As this is a GET request method in getForm of AMQMobileController will be executed.
  4. From getForm method AMQMsgProducer.produce method will be called that in turn creates 
    • AMQMobile object
    • Convert it into ObjectMessage
    • Send it to queue-one namely "amqMobileQueue"
  5. As soon as the message send to "amqMobileQueue", listener 1 i.e. AMQMobileListener will get active and its method onMessage will be executed.
  6. This is the place to perform biz logic. Now that message is send to AMQMsgSender.sendMessage method.  This method sends the message to queue-two i.e. amqMobileEmailQueue.
  7. Now Listener 2 i.e. AMQMobileEmailListener will act and its method onMessage sends the email.


Config File - applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:intg="http://www.springframework.org/schema/integration"
       xmlns:jms="http://www.springframework.org/schema/integration/jms"
       xmlns:mail="http://www.springframework.org/schema/integration/mail"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-2.1.xsd
        http://www.springframework.org/schema/integration/mail
        http://www.springframework.org/schema/integration/mail/spring-integration-mail-2.1.xsd">
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"
          p:host="domain-address"
          p:port="587"
          p:username="username"
          p:password="password"
            >
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
                <prop key="mail.smtp.auth.ntlm.domain" >DOMAIN</prop>
            </props>
        </property>
    </bean>


<bean id="reqAMQMobileQueue" class="org.apache.activemq.command.ActiveMQQueue">
        <property name="physicalName" value="amqMobileQueue" />
    </bean>

    <bean id="reqAMQMobileEmailQueue" class="org.apache.activemq.command.ActiveMQQueue">
        <property name="physicalName" value="amqMobileEmailQueue" />
    </bean>

    <bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://localhost:61616" />
    </bean>

    <bean id="amqMobileListener" class="com.examples.amqmobile.AMQMobileListener" />

    <bean id="container" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref ="activeMQConnectionFactory" />
        <property name="destination" ref ="reqAMQMobileQueue"/>
        <property name="messageListener" ref ="amqMobileListener"/>
        <property name="concurrentConsumers" value="1" />
    </bean>

    <bean id="amqMobileEmailListener" class="com.examples.amqmobile.AMQMobileEmailListener" />
    <bean id="emailContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref ="activeMQConnectionFactory" />
        <property name="destination" ref ="reqAMQMobileEmailQueue"/>
        <property name="messageListener" ref ="amqMobileEmailListener"/>
        <property name="concurrentConsumers" value="1" />
    </bean>

    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="activeMQConnectionFactory" />
        <property name="defaultDestination" ref="reqAMQMobileQueue" />
    </bean>
</beans>



Config File - main-servlet.xml



<?xml version="1.0" encoding="UTF-8"?>



<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:component-scan base-package="com" />
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
</beans>



Config File - web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">

    <display-name>Spring examples</display-name>
    <description>Spring examples</description>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/classes/applicationContext.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>main</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>main</servlet-name>
        <url-pattern>/main/*</url-pattern>
    </servlet-mapping>
</web-app>



JSP File - form.jsp (to be placed inside WEB-INF/amqmobile folder)
// This file does not contains any logic, it is just a hook point to controller

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<html>
<head>
<title>Create AMQMobile</title>
</head>
<body>

<h1>Create AMQMobile</h1>

<p><b>You have successfully created a amqmobile.</b></p>

</body>
</html>



Java File - AMQMobile.java

package com.examples.amqmobile;

import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 * User: sverma
 * Date: 18/10/12
 * Time: 3:06 PM
 */
public class AMQMobile implements Serializable {

    private String company;
    private String model;
    private int price;

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String toString () {
        StringBuilder sb = new StringBuilder().append("Company: ").append(company)
                                  .append(", Model: ").append(model)
                                  .append(", Price: ").append(price);
        return sb.toString();
    }

}


Java File - AMQMsgProducer.java
package com.examples.amqmobile;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.jms.*;

/**
 * Created with IntelliJ IDEA.
 * User: sverma
 * Date: 5/12/12
 * Time: 10:59 AM
 */

@Service("amqMsgProducer")
public class AMQMsgProducer {

    @Autowired
    JmsTemplate jmsTemplate;

    public void produce() {

        jmsTemplate.send(new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {

                AMQMobile amqMobile = new AMQMobile();
                amqMobile.setCompany("newCompany");
                amqMobile.setModel("iPhone5");
                amqMobile.setPrice(45000);

                ObjectMessage message = session.createObjectMessage(amqMobile);

                System.out.println("Sending on ["+jmsTemplate.getDefaultDestination()+"] object as " + amqMobile);

                return message;
            }
        });
    }
}


Java File - AMQMobileListener.java
package com.examples.amqmobile;

import org.apache.activemq.command.ActiveMQObjectMessage;
import org.springframework.beans.factory.annotation.Autowired;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;

/**
 * Created with IntelliJ IDEA.
 * User: sverma
 * Date: 4/12/12
 * Time: 5:18 PM
 */

public class AMQMobileListener implements MessageListener {

    @Autowired
    AMQMsgSender amqMsgSender;

    public void onMessage(Message message) {

        System.out.println("OnMessage: Listener working... " + message);
        System.out.println("OnMessage: message instance matched = " + (message instanceof ActiveMQObjectMessage));

        if (message instanceof ActiveMQObjectMessage)
        {
            try
            {
                System.out.println("Converting object to AMQMobile class.");
                AMQMobile amqMobile = (AMQMobile) ((ActiveMQObjectMessage) message).getObject();
                System.out.println("OnMessage: Object received in Listener as " + amqMobile);

                /* call message sender to put message onto second queue */
                amqMsgSender.sendMessage(amqMobile);

            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}


Java File - AMQMsgSender.java
package com.examples.amqmobile;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import javax.jms.JMSException;
import javax.jms.Queue;

/**
 * Created with IntelliJ IDEA.
 * User: sverma
 * Date: 4/12/12
 * Time: 5:21 PM
 */


@Service ("amqMsgSender")
public class AMQMsgSender {

    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private Queue reqAMQMobileEmailQueue;

    /**
     * Sends message using JMS Template.
     *
     *
     * @param msg the msg
     * @throws JMSException the jMS exception
     */
    public void sendMessage(AMQMobile msg) throws JMSException
    {
        System.out.println("About to put message on queue. Queue[" + reqAMQMobileEmailQueue.toString() + "] Message[" + msg + "]");
        jmsTemplate.convertAndSend(reqAMQMobileEmailQueue, msg);
    }

    /**
     * Sets the jms template.
     *
     * @param template the jms template
     */
    public void setJmsTemplate(JmsTemplate template)
    {
        this.jmsTemplate = template;
    }

}


File - AMQMobileEmailListener.java
package com.examples.amqmobile;

import org.apache.activemq.command.ActiveMQObjectMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

import javax.jms.Message;
import javax.jms.MessageListener;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: sverma
 * Date: 4/12/12
 * Time: 5:18 PM
 */

public class AMQMobileEmailListener implements MessageListener {

    @Autowired
    MailSender mailSender;

    public void onMessage(Message message) {

        System.out.println("OnMessage: Email Listener working... " + message);
        System.out.println("OnMessage: Email message instance matched = " + (message instanceof ActiveMQObjectMessage));

        if (message instanceof ActiveMQObjectMessage)
        {
            try
            {
                System.out.println("Converting object to AMQMobile class for email.");
                AMQMobile amqMobile = (AMQMobile) ((ActiveMQObjectMessage) message).getObject();
                System.out.println("OnMessage: Email Object received in Listener as " + amqMobile);

                /* call message sender to put message onto second queue */
                System.out.println("Sending mail for amqMobile " + amqMobile);

                SimpleMailMessage msg = new SimpleMailMessage();

                msg.setTo("sverma@impetus.co.in");

                msg.setFrom("sverma@impetus.co.in");
                msg.setSubject("Transforming from activemq");
                msg.setSentDate(new Date());
                msg.setText("Transforming using spring for mobile " + amqMobile);

                System.out.println("Waiting for 10 sec intentionally bf sending mail...");
                Thread.sleep(10000); // Mail server was throwing error of message sending limit in given time frame exceeds

                mailSender.send(msg);

            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}


Java File - AMQMobileController.java
package com.examples.amqmobile;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Created with IntelliJ IDEA.
 * User: sverma
 * Date: 18/10/12
 * Time: 7:38 PM
 */

@Controller
public class AMQMobileController {

    @Autowired
    AMQMsgProducer amqMsgProducer;

    @RequestMapping (value = "/amqmobile/form.html", method = RequestMethod.GET)
    public void getForm(Model model) {
        System.out.println("AMQController: Adding amq mobile in a model");
        amqMsgProducer.produce();
        System.out.println("AMQController: Msg send");
    }
}

Although I tried to keep my logic focus, but definitely I need to work more on formatting skills :)
I am skipping other information like maven entries etc. Please do let me know if you want any help on above.

Above Code is at GitHub, click here.


Shailendra Verma


Note: I tested this application; after commenting Thread.sleep and mail.send command to save mailbox; with jMeter for 5000 simultaneous threads and there is no exception.  All threads executed successfully.  Hurrah !!  Infact jMeter give up by outOfMemory exception :)

Tuesday, November 6, 2012

SQL Questions

Date: 06-Nov-2012

SQL: Inner / Sub query group wise problem

This blog contains questions which are different in problem solving approach and related to IT world. Origin is from day to day discussion / interview / other findings. 

Question:

Consider a schema

ID | Category | Keyword | Bid Price

Write a sql to fetch out top 5 keywords based on the bid price per category.
No RDBMS functions, only SQL.


Details/assumptions:

Table definition considered on oracle 10.x
create table test (
ID number,
Category varchar (20),
Keyword varchar (20),
BidPrice number
);

Data:

insert into test values (1, 'Category-A', 'Keyword-A1', 110);
insert into test values (2, 'Category-A', 'Keyword-A2', 121);
insert into test values (3, 'Category-A', 'Keyword-A3', 130);
insert into test values (4, 'Category-A', 'Keyword-A4', 125);
insert into test values (5, 'Category-A', 'Keyword-A5', 115);
insert into test values (6, 'Category-A', 'Keyword-A6', 133);
insert into test values (7, 'Category-B', 'Keyword-B1', 105);
insert into test values (8, 'Category-B', 'Keyword-B2', 111);
insert into test values (9, 'Category-B', 'Keyword-B3', 108);
insert into test values (10, 'Category-B', 'Keyword-B4', 128);
insert into test values (11, 'Category-B', 'Keyword-B5', 144);
insert into test values (12, 'Category-B', 'Keyword-B6', 101);
insert into test values (13, 'Category-C', 'Keyword-C1', 150);
insert into test values (14, 'Category-C', 'Keyword-C2', 137);
insert into test values (15, 'Category-C', 'Keyword-C3', 126);
insert into test values (16, 'Category-C', 'Keyword-C4', 121);
insert into test values (17, 'Category-C', 'Keyword-C5', 112);
insert into test values (18, 'Category-C', 'Keyword-C6', 118);




Output expected:

KEYWORD       CATEGORY     BIDPRICE
------------- ------------- ----------
Keyword-A3      Category-A   130
Keyword-A4      Category-A   125
Keyword-A2      Category-A   121
Keyword-A5      Category-A   115
Keyword-A1      Category-A   110
Keyword-B5      Category-B   144
Keyword-B4      Category-B   128
Keyword-B2      Category-B   111
Keyword-B3      Category-B   108
Keyword-B1      Category-B   105
Keyword-C1      Category-C   150
Keyword-C2      Category-C   137
Keyword-C3      Category-C   126
Keyword-C4      Category-C   121
Keyword-C5      Category-C   112


Answer:

Till this point all the provided ingredients are enough to cook our sql for a given scenario.  Still if you do not want to try your own and want to see result have a look:

Oracle specific solution:

SELECT KEYWORD ,CATEGORY, BIDPRICE
FROM
(
  SELECT KEYWORD ,CATEGORY, BIDPRICE,
          ROW_NUMBER() OVER (PARTITION BY CATEGORY ORDER BY BIDPRICE DESC) rn
  FROM test
) a
WHERE rn <= 5
ORDER BY CATEGORY, BIDPRICE DESC


Final Solution:
Below is the only SQL solution which is accepted as a final solution is:

SELECT 
    A.KEYWORD, A.CATEGORY, A.BIDPRICE 
FROM TEST A,  (
        SELECT 
            B.ID, B.CATEGORY 
        FROM TEST B 
        WHERE 
        (
            SELECT 
                DISTINCT(C.CATEGORY) 
            FROM TEST C 
            WHERE 
                B.CATEGORY=C.CATEGORY
        ) = B.CATEGORY 
        ORDER BY B.BIDPRICE DESC
    ) D WHERE A.ID IN 
        (SELECT ID FROM 
            (SELECT E.ID, E.CATEGORY FROM TEST E ORDER BY E.BIDPRICEDESC) F 
        WHERE A.CATEGORY=F.CATEGORY AND ROWNUM<=5
    ) 
GROUP BY A.CATEGORY, A.BIDPRICE, A.KEYWORD ORDER BY A.CATEGORY, A.BIDPRICE DESC;




Please let me know if you have any question.

Thanks
Shailendra