Message driven POJO with spring. Can't receive the message - spring

my application deployed on Tomcat am I am trying to send and receive message from remote queue.
I already have succeeded to send few messages to remote queue. Now I am trying to build message listener container, however my onMessage(method is never called), don't understand what I am missing.
This is my configuration
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destinationResolver" ref="destinationResolver" />
</bean>
<bean id="connectionFactory" class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
<property name="targetConnectionFactory" ref="targetConnectionFactory"/>
<property name="username" value="user"/>
<property name="password" value="pass"/>
</bean>
<bean id="targetConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="jndiName" value="MY_TEST_QUEUE" />
<property name="proxyInterface" value="javax.jms.ConnectionFactory"/>
</bean>
<bean id="destinationResolver" class="org.springframework.jms.support.destination.JndiDestinationResolver">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="cache" value="true" />
</bean>
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
<prop key="java.naming.provider.url">t3://host:port</prop>
</props>
</property>
</bean>
<bean id="messageListener" class="com.package.MyCustomMDB" />
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destinationName" value="MY_TEST_QUEUE"/>
<property name="destinationResolver" ref="destinationResolver" />
<property name="messageListener" ref="messageListener" />
</bean>
And implementation of mdb
public class MyCustomMDB implements MessageListener {
#Override
public void onMessage(Message message) {
System.out.println(message.toString());
}}
Could you please advise me where I am doing something wrong?

You are using MY_TEST_QUEUE for the both the jndiName of targetConnectionFactory and destinationName of jmsContainer. The the jndiName of targetConnectionFactory should refer to the name of a connection factory rather than a queue.

Related

Manage Hibernate and Activiti with Common TransactionManager

I want to use a common transaction manager (JpaTransactionManager) for hibernate and activiti, but i can not! And i have read all internet resources for that! Hear is a simple scenario (which does not even use hibernate!!):
Save variables in task
Save variable in task#execution
Complete task
Scenario implementation (in a spring bean method with #Transactional annotation):
#Component
public class TaskManager {
#Autowired TaskService taskService;
#Autowired RuntimeService runtimeService;
#Transactional
public void completeTask(CompleteTaskRequest request) {
Task task = taskService.createTaskQuery().taskId(request.getTaskId()).singleResult();
if (task == null) {
throw new ActivitiObjectNotFoundException("No task found");
}
taskService.setVariableLocal(task.getId(), "actionDisplayUrl", request.getActionDisplayUrl());
taskService.setVariableLocal(task.getId(), "actionSummaryUrl", request.getActionSummaryUrl());
runtimeService.setVariableLocal(task.getExecutionId(), "prevTaskId", task.getId());
taskService.complete(task.getId());
}
}
It's obvious: if taskService.complete throws error, the whole transaction should be rollbacked, so all saved variables should be rollbacked, and the below test case should be passed:
#Test
#Deployment(resources = "org.activiti.test/CompleteTaskTest.bpmn20.xml")
public void testCompleteTaskWithError() {
Map<String, Object> processVars = new HashMap<>();
processVars.put("error", true); // Causes throwing error in ScriptTaskListener
runtimeService.startProcessInstanceByKey("CompleteTaskTest", processVars);
Task task = taskService.createTaskQuery().taskName("Task 1").singleResult();
CompleteTaskRequest req = new CompleteTaskRequest();
req.setTaskId(task.getId());
req.setActionDisplayUrl("/actions/1234");
req.setActionSummaryUrl("/actions/1234/summary");
try {
taskManager.completeTask(req);
fail("An error expected!");
} catch(Exception e) {
}
// Check variables rollback
assertNull(taskService.getVariableLocal(task.getId(),"actionSummaryUrl"));
assertNull(taskService.getVariableLocal(task.getId(),"actionDisplayUrl"));
assertNull(runtimeService.getVariableLocal(task.getExecutionId(), "prevTaskId"));
}
But it fails, the variables are committed to DB (not rollbacked).
Spring context (Using org.springframework.orm.jpa.JpaTransactionManager as transaction manager):
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="idGenerator" ref="idGenerator"/>
<property name="databaseSchemaUpdate" value="true" />
<property name="jpaEntityManagerFactory" ref="entityManagerFactory" />
<property name="jpaHandleTransaction" value="true" />
<property name="jpaCloseEntityManager" value="true" />
<property name="beans" ref="processEngineBeans" />
<property name="jobExecutorActivate" value="false" />
<property name="asyncExecutorEnabled" value="true" />
<property name="asyncExecutorActivate" value="true" />
</bean>
<aop:config proxy-target-class="true" />
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean>
<bean id="processEngineBeans" class="java.util.HashMap">
<constructor-arg index="0" type="java.util.Map">
<map>
</map>
</constructor-arg>
</bean>
<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
<bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />
<bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" />
<bean id="formService" factory-bean="processEngine" factory-method="getFormService" />
<bean id="idGenerator" class="org.activiti.engine.impl.persistence.StrongUuidGenerator" />
<bean id="activitiRule" class="org.activiti.engine.test.ActivitiRule">
<property name="processEngine" ref="processEngine" />
</bean>
<bean id="persistenceUnitManager"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="packagesToScan" value="org.activiti.test" />
<property name="defaultDataSource" ref="dataSource" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager" />
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect_resolvers">org.hibernate.engine.jdbc.dialect.internal.DialectResolverSet</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<!-- bean post-processor for JPA annotations -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" >
<property name="proxyTargetClass" value="true" />
</bean>
Versions:
Activiti version: 5.22.0
DB: h2 (Also tested with Oracle in production)
What is wrong with my configurations?
P.S.
The test project is uploaded here.
I have also asked this question in alfresco forum.
UPDATE::
By using org.springframework.jdbc.datasource.DataSourceTransactionManager instead of org.springframework.orm.jpa.JpaTransactionManager the test case passed. But now i can not persist JPA entities.
The problem is solved by resolving conflict between MyBatis (JDBC) and Hibernate (JPA):
jpaVendorAdapter property should be added to entityManagerFactory bean:
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
So entityManagerFactory bean should be like this:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager" />
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider" />
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect_resolvers">org.hibernate.engine.jdbc.dialect.internal.DialectResolverSet</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
For more details see answer of this question.

WebLogic 10.3.6 throws JMSClientExceptions:055142 when configured with lookupOnStartup false

I have simple Java Spring application which looks up JMS objects using JNDI and publishes a message to a JMS Topic. JNDI and JMS configured on WebLogic 10.3.6. All this works fine as long as the WebLogic server is up and running.
I need to get the application to start up even when the WebLogic server is down. I have configured the JNDI objects with "lookupOnStartup" as "false".
Below is my Spring configuration.
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">${jndi.initialFactory}</prop>
<prop key="java.naming.provider.url">${jndi.providerurl}</prop>
</props>
</property>
</bean>
<bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="jndiTemplate" />
</property>
<property name="jndiName">
<value>${jms.connectionFactory}</value>
</property>
<property name="lookupOnStartup" value="false" />
<property name="proxyInterface" value="javax.jms.ConnectionFactory" />
</bean>
<bean id="myTopic" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="jndiTemplate" />
</property>
<property name="jndiName">
<value>${jms.mytopic}</value>
</property>
<property name="lookupOnStartup" value="false" />
<property name="proxyInterface" value="javax.jms.Destination" />
</bean>
<bean id="jmsDestinationResolver"
class="org.springframework.jms.support.destination.JndiDestinationResolver">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="cache" value="true" />
</bean>
<bean id="myTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="defaultDestination" ref="myTopic" />
<property name="destinationResolver" ref="jmsDestinationResolver" />
<property name="connectionFactory" ref="connectionFactory" />
<property name="sessionAcknowledgeModeName" value="AUTO_ACKNOWLEDGE" />
<property name="sessionTransacted" value="false" />
</bean>
At runtime I get the following exception:
Exception in thread "main" org.springframework.jms.InvalidDestinationException: [JMSClientExceptions:055142]Foreign destination, jmsserver-module!my-topic; nested exception is weblogic.jms.common.InvalidDestinationException: [JMSClientExceptions:055142]Foreign destination, jmsserver-module!my-topic
at org.springframework.jms.support.JmsUtils.convertJmsAccessException(JmsUtils.java:285)
at org.springframework.jms.support.JmsAccessor.convertJmsAccessException(JmsAccessor.java:169)
at org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:497)
at org.springframework.jms.core.JmsTemplate.send(JmsTemplate.java:569)
...
Caused by: weblogic.jms.common.InvalidDestinationException: [JMSClientExceptions:055142]Foreign destination, jmsserver-module!my-topic
at weblogic.jms.common.Destination.checkDestinationType(Destination.java:105)
at weblogic.jms.client.JMSSession.setupJMSProducer(JMSSession.java:2830)
at weblogic.jms.client.JMSSession.createProducer(JMSSession.java:2858)
at weblogic.jms.client.JMSSession.createProducer(JMSSession.java:2822)
at weblogic.jms.client.WLSessionImpl.createProducer(WLSessionImpl.java:827)
at org.springframework.jms.core.JmsTemplate.doCreateProducer(JmsTemplate.java:1143)
at org.springframework.jms.core.JmsTemplate.createProducer(JmsTemplate.java:1124)
at org.springframework.jms.core.JmsTemplate.doSend(JmsTemplate.java:601)
at org.springframework.jms.core.JmsTemplate$3.doInJms(JmsTemplate.java:572)
at org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:494)
... 3 more
Any help is much appreciated. Thank you!
In Destination.java:
if(!(destination instanceof DestinationImpl))
throw new InvalidDestinationException
The proxied connectionFactory is not able to resolve the the destination type. I was able to resolve this issue by providing only the destination name to the JmsTemplate and using a JNDI destinationResolver to resolve the destination type on demand. So the modified Spring configuration looks like:
<bean id="myTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="defaultDestinationName">
<value>${jms.mytopic}</value>
</property>
<property name="destinationResolver" ref="jmsDestinationResolver" />
<property name="connectionFactory" ref="connectionFactory" />
<property name="sessionAcknowledgeModeName" value="AUTO_ACKNOWLEDGE" />
<property name="sessionTransacted" value="false" />

Camel Transaction Configuration Issue: javax.persistence.TransactionRequiredException

I'm configuring JMS Transactions in Camel 2.10.4 routes. When I run my app, a javax.persistence.TransactionRequiredException: no transaction in progress is thrown. From my research, I found out that this exception is thrown when no method is marked #Transactional. The relevant sections in my application context XML config file is shown:
<bean id="txMgr" class="org.springframework.jms.connection.JmsTransactionManager">
<property name="connectionFactory" ref="pooledConnectionFactory" />
</bean>
<bean id="REQUIRED" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
<property name="transactionManager" ref="txMgr" />
<property name="propagationBehaviorName" value="PROPAGATION_REQUIRED" />
</bean>
<bean id="pooledConnectionFactory"
class="org.apache.activemq.pool.PooledConnectionFactory" init-method="start" destroy-method="stop">
<property name="maxConnections" value="8" />
<property name="connectionFactory" ref="jmsConnectionFactory" />
</bean>
<bean id="jmsConnectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616" />
</bean>
<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="configuration" ref="jmsConfig" />
</bean>
<bean id="jmsConfig"
class="org.apache.camel.component.jms.JmsConfiguration">
<property name="connectionFactory" ref="pooledConnectionFactory"/>
<property name="transacted" value="true" />
<property name="transactionManager" ref="txMgr" />
<property name="concurrentConsumers" value="3"/>
</bean>
Besides the JMS-specific configuration, I also have JPA configuration, which is shown below:
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager">
<bean class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</property>
</bean>
<bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="fileRecord" />
</bean>
If I disable transactions, the configuration works. When I enable it, however, all the other steps succeeds except the bit where the data is to be inserted into the database (at the JPA endpoint).
Any suggestions on what I need to change or add will be appreciated very much.

Connect to EMS JMS queue using Spring3 + JNDI

I'm having some issues create a connection to (and reading from) a Tibco EMS JMS queue.
<beans>
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">com.tibco.tibjms.naming.TibjmsInitialContextFactory</prop>
<prop key="java.naming.provider.url">tcp://ems-dit-am-uat-1.app.xxx.net:30055</prop>
</props>
</property>
</bean>
<bean id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate" /> <property name="jndiName"
value="DRDRFIQueueConnectionFactory" /> </bean>
<bean id="jmsDestinationResolver"
class="org.springframework.jms.support.destination.JndiDestinationResolver">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="cache" value="true" />
</bean>
<bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="jndiName" value="Q.NY.DERIV.DRD.RFI" />
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="destinationResolver" ref="jmsDestinationResolver" />
<property name="defaultDestination" ref="destination" />
</bean>
<bean id="jmsReceiver" class="com.csfb.fao.rds.rfi.application.DRDReceiverTst">
<property name="jmsTemplate">
<ref bean="jmsTemplate" />
</property>
</bean>
</beans>
The exception I'm getting is:
javax.naming.AuthenticationException: Not permitted: invalid name or
password [Root exception is javax.jms.JMSSecurityException: invalid
name or password] at
com.tibco.tibjms.naming.TibjmsContext.lookup(TibjmsContext.java:668)
at
com.tibco.tibjms.naming.TibjmsContext.lookup(TibjmsContext.java:489)
at javax.naming.InitialContext.lookup(InitialContext.java:392) at
org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:154)
at
org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)
at
org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152)
at
org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178)
at
org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95)
at
org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105)
at
org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:201)
at
org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:187)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
... 12 more
The only user/password I've been given is for the JMS queue itself - where do I set that?
Thanks
Chris
Got it - needed to wrap the connection factory in a UserCredentialsConnectionFactory:
<bean id="authenticationConnectionFactory"
class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
<property name="targetConnectionFactory" ref="jmsConnectionFactory" />
<property name="username" value="yyyyy" />
<property name="password" value="xxxx" />
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="authenticationConnectionFactory" />
<property name="destinationResolver" ref="jmsDestinationResolver" />
<property name="defaultDestination" ref="destination" />
I had some similar problem , solution was to add (besides solution from this question)
<prop key="java.naming.security.principal">username</prop>
<prop key="java.naming.security.credentials">password</prop>
to jndiTemplate bean configuration
I don't have any experience with EMS, but user and password are typically set on the connection factory, so you'd want to configure that on the object being provided by JNDI.

how to configure jms template at spring for weblogic?

as my question title, how to configure jms template at spring for weblogic?
i follow an example at some website, but spring always complain about defaultDestination at JmsTemplate
how to configure it correctly ?
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
<prop key="java.naming.provider.url">t3://localhost:7001</prop>
</props>
</property>
</bean>
<bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="jndiName" value="jms/confactory" />
</bean>
<bean id="jmsDestinationResolver" class="org.springframework.jms.support.destination.JndiDestinationResolver">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="cache" value="true" />
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destinationResolver" ref="jmsDestinationResolver" />
</bean>
nb : i use weblogic 9.2 for jms & web server, spring 2.5.6
i find out, that destination should contain jms destination
<bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jms/queue" />
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destinationResolver" ref="jmsDestinationResolver" />
<property name="defaultDestination" ref="destination" />
<property name="sessionAcknowledgeModeName" value="CLIENT_ACKNOWLEDGE"/>
<property name="sessionTransacted" value="true" />
</bean>

Resources