message-queue-config.spring.xml
-------------------------------
<!-- Jndi properies for accessing Jboss 7.1 Hornet Queue -->
<bean id="jnditemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">org.jboss.naming.remote.client.InitialContextFactory
</prop>
<prop key="java.naming.provider.url">remote://${sampleweb.server.ip}:4447</prop>
<prop key="java.naming.security.principal">uanmcts</prop>
<prop key="java.naming.security.credentials">password</prop>
</props>
</property>
</bean>
<!-- Jms connection configurations -->
<bean id="connectionfactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jnditemplate" />
<property name="jndiName" value="jms/RemoteConnectionFactory" />
</bean>
<!-- Jms queue configurations -->
<bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jnditemplate" />
<property name="jndiName" value="jms/queue/mail" />
</bean>
<!-- Credentials for accessing jboss queue -->
<bean id="credentialsconnectionfactory"
class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
<property name="targetConnectionFactory" ref="connectionfactory" />
<property name="username" value="uanmcts" />
<property name="password" value="password" />
</bean>
<bean id="cachingConnectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="connectionfactory" />
<property name="sessionCacheSize" value="1" />
</bean>
<!-- Jms template configurations -->
<bean id="jmstemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="cachingConnectionFactory" />
<property name="defaultDestination" ref="destination" />
</bean>
<bean id="messageService"
class="com.jkcs.uanmcts.core.messaging.service.impl.EmailMessageServiceImpl">
<property name="jmsTemplate" ref="jmstemplate" />
</bean>
<!-- Jms message Listner configurations -->
<bean
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="credentialsconnectionfactory" />
<property name="destination" ref="destination" />
<property name="messageListener" ref="messageService" />
<property name="autoStartup" value="true" />
<property name="concurrentConsumers" value="5" />
<property name="maxConcurrentConsumers" value="10" />
</bean>
Refer bean 'messageService'
----------------------------
@InternalService
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public class EmailMessageServiceImpl implements EmailMessageService {
private static final Log LOG = LogFactory
.getLog(EmailMessageServiceImpl.class);
@Autowired
private JmsTemplate jmsTemplate;
@Autowired
JKCSMailSender emailSender;
@Override
public Boolean sendEmailToQueue(final TemplatedEmailDTO messageDto)
throws FrameworkServiceException {
Boolean isEmailSent = false;
LOG.info("Trying to send email message on queue.");
try {
getJmsTemplate().send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
ObjectMessage objectMessage = session.createObjectMessage(messageDto);
objectMessage.setJMSRedelivered(false);
objectMessage.setJMSExpiration(0); /*Value of 0 indicates that a message will never expire*/
return objectMessage;
}
});
isEmailSent = true;
LOG.info("Email message has been successfully sent to the queue.");
} catch (Exception e) {
isEmailSent = false;
LOG.info("Email message failed while sending to the queue.");
}
return isEmailSent;
}
@Override
public void onMessage(Message message) {
LOG.info("Email message received from queue [" + message +"]");
try {
if(message!=null)
{
message.acknowledge();
if (message instanceof ObjectMessage) {
ObjectMessage objectMessage = (ObjectMessage) message;
TemplatedEmailDTO emailDto = (TemplatedEmailDTO) objectMessage.getObject();
emailSender.send(emailDto);
LOG.info("Email message has been successfully sent.");
}
}
else
{
LOG.info("Null email message received.");
}
} catch (JMSException jmsEx_p) {
String errMsg = "An error occurred extracting message";
LOG.error(errMsg, jmsEx_p);
throw new FrameworkServiceException();
}
}
public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
}