Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package service.emailer; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.JMSException; import javax.jms.MapMessage; import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; import javax.naming.InitialContext; import javax.naming.NamingException; /** * * @author Chunsing */ public class Emailer { private static final String JNDI_CONNECTION_FACTORY = "jms/__defaultConnectionFactory"; private static final String JNDI_QUEUE = "jms/emailQueue"; private static <T> T lookup(Class<T> retvalClass, String jndi) { try { return retvalClass.cast(InitialContext.doLookup(jndi)); } catch (NamingException ex) { throw new IllegalArgumentException("failed to lookup instance of " + retvalClass + " at " + jndi, ex); } } /** * Send email with parameters to emailQueue * @param area * @param email */ public static void sendEmail(String area, List<String> email) { try { //JMS QUEUE SEND final ConnectionFactory connectionFactory = lookup(ConnectionFactory.class, JNDI_CONNECTION_FACTORY); final Queue queue = lookup(Queue.class, JNDI_QUEUE); Connection connection = connectionFactory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(queue); MapMessage message = session.createMapMessage(); message.setString("type", "warnFile"); message.setString("area", area); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(email); message.setString("emailReceivers", json); producer.send(message); } catch (JMSException ex) { Logger.getLogger(Emailer.class.getName()).log(Level.SEVERE, null, ex); } catch (JsonProcessingException ex) { Logger.getLogger(Emailer.class.getName()).log(Level.SEVERE, null, ex); } } }