Saturday, October 19, 2013

JMS


The Java Message Service is a Java API that allows applications to create, send, receive, and read messages.
The JMS API defines a common set of interfaces and associated semantics that allow programs written in the Java programming language to communicate with other messaging implementations.
The JMS API enables communication that is not only loosely coupled but also,
  • Asynchronous: A JMS provider can deliver messages to a client as they arrive; a client does not have to request messages in order to receive them.
  • Reliable: The JMS API can ensure that a message is delivered once and only once. Lower levels of reliability are available for applications that can afford to miss messages or to receive duplicate messages.


import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import javax.jms.Session;

public class AsyncMessageServiceImpl implements MessageService {

private JmsTemplate jmsTemplate;
private Destination destination;

public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}

public void setDestination(Destination destination) {
this.destination = destination;
}

@Override
public void postMessage(final Map<String, Object> aMap) {
jmsTemplate.send(destination, new MessageCreator() {
@SuppressWarnings("unchecked")
public Message createMessage(Session session) throws JMSException {
ObjectMessage objectMessage = session.createObjectMessage();
objectMessage.setObject((HashMap) aMap);
return objectMessage;

}
});
}

}


public class MessageMDB implements MessageListener {
@Override
public void onMessage(Message message) {
Map msg = null;
if (!(message instanceof ObjectMessage)) {
return;
}
ObjectMessage objectMessage = (ObjectMessage) message;
Map<String,String> map=new HashMap<String, String>();
try {
map=(Map<String, String>) objectMessage.getObject();
} catch (JMSException e) {
e.printStackTrace();
}
mailService.sendMail(map);

}
}

//=================================================
java.security.Security
.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

Properties props = new Properties();

// Set the host smtp address
props.put("mail.smtp.host", hostName);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");

Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, passWord);
}
};

javax.mail.Session sess = javax.mail.Session.getInstance(props,
auth);

// boolean debug = false;
// sess.setDebug(debug);

// create a message

MimeMessage msg = new MimeMessage(sess);

  • ConnectionFactory – This is the object a client uses to create a connection with a provider.
  • Destination – This is the object a client uses to specify the destination of messages it is sending and the source of messages it receives.








JMS API Programming









Reference doc

No comments:

Security Certificates

  1. Cryptography Basics Understand Key Concepts : Encryption, decryption, hashing, and digital signatures. Key terms: confidentiality, inte...