Spring JMS Publish/Susbcriber configuration to IBM Webpshere MQ - spring

Searching in the web I found a lot of example of how spring have to be configured (file.xml) for publish/subscriber mode for active MQ but I need how beans need to be configured for Websphere MQ
Anyone have been configured both product with success?

The following is bean definition of a Websphere MQ ConnectionFactory:
<bean id="wmqConnectionFactory" class="com.ibm.mq.jms.MQConnectionFactory">
<property name="queueManager">
<value>QUEUE_MANAGER</value>
</property>
<property name="hostName">
<value>127.0.0.1</value>
</property>
<property name="port">
<value>1414</value>
</property>
<property name="transportType">
<value>1</value>
</property>
<property name="channel">
<value>SYSTEM.DEF.SVRCONN</value>
</property>
</bean>
you may need to provide a username and password, and you can accomplish that by wrapping it in a UserCredentialsConnectionFactoryAdapter:
<bean id="userCredentialsAdapter" class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
<property name="targetConnectionFactory" ref="wmqConnectionFactory" />
<property name="username" value=" " />
<property name="password" value=" " />
</bean>
It is also a good idea to wrap it in a CachingConnectionFactory, so that sessions, consumers and producers are cached, this wrapper also switches the auto-recovery to true:
<bean id="cachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="userCredentialsAdapter"/>
<property name="cacheConsumers" value="true" />
<property name="cacheProducers" value="true" />
<property name="sessionCacheSize" value="10" />
</bean>
with this configuration, you can use the cachingConnectionFactory with whatever bean you need.

Related

Spring: JMSTemplate/CachingConnectionFactory deployables unable to start automatically in weblogic

I recently changed some of my application to use the the following:
org.springframework.jndi.JndiTemplate
org.springframework.jms.connection.CachingConnectionFactory
org.springframework.jms.core.JmsTemplate
Everything is working fine and I'm able to deploy my war files and send JMS messages to the queue.
However something peculiar happens when my managed server restarts. The deployables will all go into a fail state which requires me to then manually start them up.
This started happening after the change to use caching connection factory, jndi template and jms template.
My SpringConfig file:
<!-- Service Controller begin -->
<bean id="appUtils" class="com.foo.util.AppUtil" lazy-init="true" />
<bean id="jms_jndiTemplate" class="org.springframework.jndi.JndiTemplate" lazy-init="true">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">#{jmsJndiFactory}</prop>
<prop key="java.naming.provider.url">#{jmsIp}</prop>
</props>
</property>
</bean>
<bean id="jmsUtils" class="com.foo.JmsUtil" >
<property name="template">
<bean class="org.springframework.jms.core.JmsTemplate" lazy-init="true">
<property name="connectionFactory">
<bean class="org.springframework.jms.connection.CachingConnectionFactory" lazy-init="true">
<property name="sessionCacheSize" value="10" />
<property name="targetConnectionFactory">
<bean class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jms_jndiTemplate" />
<property name="jndiName" ref="jmsFactory" />
</bean>
</property>
</bean>
</property>
</bean>
</property>
<property name="destination">
<bean class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true">
<property name="jndiTemplate" ref="jms_jndiTemplate" />
<property name="jndiName" ref="jmsQueue" />
</bean>
</property>
</bean>
ApplicationContext file:
<bean id="jmsQueue" class="java.lang.String" ><constructor-arg value="${jmsQueue.local}" /></bean>
<bean id="jmsFactory" class="java.lang.String" ><constructor-arg value="${jmsFactory.local}" /></bean>
<bean id="jmsJndiFactory" class="java.lang.String" ><constructor-arg value="${jmsJndiFactory.local}" /></bean>
<bean id="jmsIp" class="java.lang.String" ><constructor-arg value="${jmsIp.local}" /></bean>
applicationProperties file:
jmsQueue.local=jms/Queue
jmsFactory.local=jms/ConnectionFactory
jmsJndiFactory.local=weblogic.jndi.WLInitialContextFactory
jmsIp.local=t3://localhost:7031
Anyone has any idea as to why this might be happening? I'm using Weblogic. Any help would be greatly appreciated.
Thanks!
Edit: Forgot to mention that the error causing the failed state is
javax.naming.NameNotFoundException: Unable to resolve 'jms.Queue'. Resolved 'jms'; remaining name 'Queue'.
This is a JNDI error. The message means "I tried to find jms/Queue in the JNDI context but I only got as far as jms; there is no Queue child below".
Check the resources which you configured for the application in WebSphere.

How to configure a Spring 3 consumer for activemq 5 over SSL

I am trying to create an SSL connection to activemq (using Spring 3.1, ActiveMQ 5.5, and Camel 10.0). I'm getting the dreaded SSL handshake exception. I can connect with an openssl s_client using the certificate in the jks. Thus, I'm trying to figure out if there is a problem with my keystore (which seems to work in other situations - e.g. with tomcat) or a problem with my XML configuration. Does anyone have a good example, thoughts about other ways to test, or see what I'm doing wrong?
Please note, my config passes validation (in case you see a typo).
Thanks
-J
<bean id="myJmsRedeliverPolicy" class="org.apache.activemq.RedeliverPolicy">
<property name="maximumRedeliveries" value="500"/>
</bean>
<bean id="jmsSecureConnectionFactory" class="org.apache.activemq.ActiveMQSslConnectionFactory">
<property name="brokerURL" value="ssl://test.com:8100"/>
<property name="redeliverPolicy" ref="myJmsRedeliverPolicy"/>
<property name="keyStore" value="/usr/lib/mykeystore.jks"/>
<property name="keyStorePassword" value="mypass"/>
<property name="trustStore" value="/usr/lib/mycacerts"/>
<property name="trustStorePassword" value="changeit"/>
</bean>
<bean id="pooledSecureConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
<property name="maxConnections" value="10"/>
<property name="connectionFactory" ref="jmsSecureConnectionFactory"/>
</bean>
<bean id="txSecureManager" class="org.springframework.jms.connection.JmsTransactionManager">
<property name="connectionFactory" ref="pooledSecureConnectionFactory" />
</bean>
<bean id="jmsSecureConfig" class="org.apache.camel.component.jms.JmsConfiguration">
<property name="connectionFactory" ref="pooledSecureConnectionFactory" />
<property name="testConnectionOnStartup" value="true"/>
<property name="transacted" value="true"/>
<property name="transactionManager" ref="txSecureManager"/>
</bean>
<bean id="activemqs" class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="configuration" ref="jmsSecureConfig" />
</bean>
Start your application with:
-Djavax.net.debug=ssl
to get further with your troubleshooting. Usually, that command provides printout that says pretty much exactly what's wrong.

Tomcat deploy my project twice

Tomcat deploy my project twice. is anybody faced and solved this issue?. This is spring project and I am using quartz scheduler. because of this problem quartz running twice.
Using Quartz-2.1.1 and Spring framework 3.1.1
The quartz configuration is as follows :
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail" />
<property name="startDelay" value="10000" />
<property name="repeatInterval" value="1000" />
</bean>
<bean id="sc" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
</bean>
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="staFromInQObj" />
<property name="targetMethod" value="processInQueueData" />
<property name="concurrent" value="false" />
</bean>
I think this is the correct answer for the problem.
Spring 3 MVC dispatcher xml and applicationContext xml
Double deployments can happen due to spring configs as well !

EJB 3.0 -> Spring -> JPA (JTA as transaction manager)

I am currently working on a project that includes EJB 3.0 (stateless SB), JPA (Hibernate as the provider), JTA as transaction manager. The app server is JBoss AS 7. Spring is used for integrating EJB and JPA.
All seems to be working fine, except if there is any exception that occurs in the EJB, then the persistence unit is closed by Spring. On the subsequent request, the persistence unit is again created, which becomes time consuming and also should not happen in the ideal situation.
Below are the configuration details
persistence.xml
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<class>com.test.User</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
spring-application-context.xml
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<jee:jndi-lookup id="dataSource" jndi-name="java:/datasources/test" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="false" />
<property name="database" value="MYSQL" />
<property name="showSql" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"></entry>
<entry key="hibernate.current_session_context_class" value="jta" />
<entry key="hibernate.connection.release_mode" value="auto" />
</map>
</property>
<property name="persistenceUnitPostProcessors">
<list>
<bean class="com.transaction.processor.JtaPersistenceUnitPostProcessor">
<property name="jtaMode" value="true"/>
<property name="jtaDataSource" ref="dataSource"/>
</bean>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManagerName" value="java:/TransactionManager"></property>
<property name="autodetectUserTransaction" value="false"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
The class JtaPersistenceUnitPostProcessor is responsible for setting the transaction-type as JTA and the datasource to jta-datasource.
Could anyone please provide any help on this.
Thanks in advance.
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManagerName" value="java:jboss/TransactionManager" />
<property name="userTransactionName" value="java:comp/UserTransaction" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
you didn't specify any error message . you can add these lines in your configuration file .
I see you use JTA transaction manager and use that only if you use distributed Transaction and use JNDI. JTA tran. manager listens TX happening through connection acquired from JNDI datasource. If you have datasource created in your code and is not a part of Web container but is limited inside app. container in your web server, JTA wont work.
If you want to implement Tx manager with in a single app. context go for JPA transaction manager which is very reliable.

Spring/JTA/JPA DAO integration test doesn't rollback?

My DAO integration tests are failing because entities created during the tests are still in the database at the start of the next test. The exact same behavior is seen from both MySQL 5 and H2.
The test classes are annotated with:
#Transactional
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration( { "/testPersist-applicationContext.xml" })
The transaction bean configuration in the test application context is as follows:
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="atomikosTransactionManager" />
<property name="userTransaction" ref="atomikosUserTransaction" />
</bean>
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
init-method="init" destroy-method="close">
<property name="forceShutdown" value="false" />
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<property name="transactionTimeout" value="300" />
</bean>
The entity manager is configured as follows:
<bean id="myappTestLocalEmf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="myapp" />
<property name="persistenceUnitPostProcessors">
<bean class="com.myapp.core.persist.util.JtaPersistenceUnitPostProcessor">
<property name="jtaDataSource" ref="myappPersistTestJdbcDataSource" />
</bean>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
<property name="database" value="$DS{hibernate.database}" />
<property name="databasePlatform" value="$DS{hibernate.dialect}" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup
</prop>
<prop key="hibernate.format_sql">true"</prop>
<prop key="hibernate.use_sql_comments">true</prop>
</props>
</property>
</bean>
<context:annotation-config />
Everything in the log files seem to be fine...I can see messages from Spring about rollback and from Atomikos about rollback as well. But frankly, the logs are so huge and so complex, I could easily be missing something...
Yet the inserted test data remains! Any clues?
It turned out that my C3P0 JDBC data source was not XA aware and was therefore not participating in the transaction. Why I did not get an error, nor a warning in the log file, I do not know. Nonetheless, the reason you cannot use an XA aware data source is explained very nicely here. Note that the data source does not need to be XA capable...just XA aware.
Replacing the C3P0 data source with the following one solved the problem.
<bean id="myappJTANonXADataSource" class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean">
<property name="uniqueResourceName" value="myappDatabase" />
<property name="driverClassName" value="$DS{hibernate.connection.driver_class}" />
<property name="url" value="$DS{hibernate.connection.url}" />
<property name="user" value="$DS{hibernate.connection.username}" />
<property name="password" value="$DS{hibernate.connection.password}" />
<property name="maxPoolSize" value="20" />
<property name="reapTimeout" value="300" />
</bean>
I think you will need to go through the logs in details. It could be that the rollbacks you are seeing are working, except that something else has executed a commit first. I also cannot see anything in your code which indicates automated rollback. And that it should occur at the end of each test. If you are depending on a timeout based rollback it could be that the second test is running before the timeout occurs, therefore it sees the data before it is rolled back.
Many options here there is :-)

Resources