How to assign SourcePollingChannelAdapter in application file - java

Below is spring's application file & i have defined inbound channel in that.
Accessing that channel by initializing the object of SourcePollingChannelAdapter.
But i want to access that object using the reference from another bean.
How can i do that. Can someone please guide me.
Something like this:
<bean id="DataAccessController"
class="com.canaldigital.tsi.dao.controller.DataAccessControllerImpl">
<property name="sftpAdapterAutoCreate" ref="sftpAdapterAutoCreate" />
</bean>
ApplicationContext.xml
<bean id="defaultSftpSessionFactory"
class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
<property name="host" value="${sftp.host}"/>
<property name="user" value="${sftp.username}"/>
<!-- <property name="password" value="${sftp.password}"/> -->
<property name="port" value="${sftp.serverPort}"/>
<!-- <property name="privateKey" value="${sftp.private.keyfile}"/> -->
<property name="privateKey" value="classpath:IBS_KEYS/id_rsa.txt"/>
<property name="privateKeyPassphrase" value="${sftp.passphrase}"/>
</bean>
<bean id="sftpSessionFactory" class="org.springframework.integration.file.remote.session.CachingSessionFactory">
<constructor-arg ref="defaultSftpSessionFactory" />
<!-- <property name="sessionCacheSize" value="10"/>
<property name="sessionWaitTimeout" value="1000"/> -->
</bean>
<int-sftp:inbound-channel-adapter id="sftpAdapterAutoCreate"
session-factory="sftpSessionFactory"
channel="requestSFTPDKDEVChannel"
filename-pattern="*.*"
remote-directory="/home/oracle/"
preserve-timestamp="true"
local-directory="C:/temp/"
auto-create-local-directory="true"
temporary-file-suffix=".writing"
delete-remote-files="true">
<int:poller fixed-rate="1000" time-unit="SECONDS" />
</int-sftp:inbound-channel-adapter>
<int:channel id="requestSFTPDKDEVChannel">
<int:queue/>
</int:channel>
Test.java
SourcePollingChannelAdapter adapter = context1
.getBean("sftpAdapterAutoCreate",SourcePollingChannelAdapter.class);
adapter.start();

Related

add redeliveryPolicy to Websphere MQ message

I have a Websphere MQ and a java app receiveng messages from it. I want to make redelivering system if any exceptions is thrown .
Is there a way to add redeliveryDelay in my configuration spring xml?
here's my spring configuration:
<!-- JMS CONNECTION FACTORY -->
<bean id="MQFactory" class="com.ibm.mq.jms.MQConnectionFactory">
<property name="transportType">
<util:constant static-field="com.ibm.msg.client.wmq.WMQConstants.WMQ_CM_CLIENT" />
</property>
<property name="queueManager" value="${queueManager}" />
<property name="hostName" value="${hostName}" />
<property name="port" value="${port}" />
<property name="channel" value="${channel}" />
</bean>
<bean id="JmsConnectionFactory"
class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
<property name="targetConnectionFactory" ref="MQFactory" />
<property name="username" value="${username}" />
<property name="password" value="${username}" />
</bean>
<!-- JMS LISTENER -->
<bean id="Listener" class="jms.impl.Listener"></bean>
<!-- JMS CONTAINER -->
<bean id="JmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="JmsConnectionFactory" />
<property name="destinationName" value="${destination}" />
<property name="messageListener" ref="Listener" />
<property name="autoStartup" value="false" />
<property name="concurrentConsumers" value="${jms.consumers}" />
<property name="sessionTransacted" value="true" />
</bean>

Spring Integration Atomikos transaction issue

I'm trying to integrate Atomikos transaction manager into a Spring Integration program that forwards JMS from ActiveMQ to a WebMethods ESB.
The spring integration part only retrieves JMs from local ActiveMQ broker and sends them to a distant ESB broker.
When I test the nominal case, JMS is sent well and passes through the ESB and is dispatched to the subscribers then.
When I test the case where ESB sending fails, I have an issue : the JMS is never published back. I suppose it's a transaction issue because the transaction should have been rolled back when program tried to publish on ESB broker but it seems not.
Here's my spring config :
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="AtomikosTransactionManager" />
<property name="userTransaction" ref="AtomikosUserTransaction" />
</bean>
<!-- Atomikos Transaction Manager Defintion (JTA) -->
<bean id="AtomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
init-method="init" destroy-method="close" depends-on="atomikosConnectionFactorySource,connectionFactoryDestination">
<property name="transactionTimeout" value="300" />
<property name="forceShutdown" value="false" />
</bean>
<bean id="AtomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<property name="transactionTimeout" value="300" />
</bean>
<bean id="jmsXaConnectionFactory" class="org.apache.activemq.ActiveMQXAConnectionFactory">
<property name="brokerURL" value="${source.java.naming.provider.url}" />
<property name="userName" value="${source.username}" />
<property name="password" value="${source.passwd}" />
</bean>
<bean id="atomikosConnectionFactorySource" class="com.atomikos.jms.AtomikosConnectionFactoryBean"
init-method="init" destroy-method="close">
<property name="poolSize" value="1" />
<property name="uniqueResourceName" value="activemq" />
<property name="xaConnectionFactory" ref="jmsXaConnectionFactory" />
</bean>
<bean id="connectionFactorySource"
class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="jmsXaConnectionFactory" />
<property name="clientId" value="CustomerOrderForwarderID" />
<property name="reconnectOnException" value="true" />
</bean>
<!-- Destination JNDI Context -->
<bean id="jndiTemplateDestination" class="org.springframework.jndi.JndiTemplate"
lazy-init="true">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">${destination.java.naming.factory.initial}</prop>
<prop key="java.naming.provider.url">${destination.java.naming.provider.url}</prop>
<prop key="java.naming.factory.url.pkgs">${destination.java.naming.factory.url.pkgs}</prop>
</props>
</property>
</bean>
<!-- Destination Connection factory -->
<bean id="customerOrderXAConnectionFactoryDestination" class="org.springframework.jndi.JndiObjectFactoryBean"
lazy-init="true">
<property name="jndiTemplate" ref="jndiTemplateDestination" />
<property name="jndiName"
value="${destination.java.naming.factory.connection}" />
<property name="lookupOnStartup" value="false" />
<property name="proxyInterface" value="javax.jms.XAConnectionFactory" />
</bean>
<bean id="connectionFactoryDestination" class="com.atomikos.jms.AtomikosConnectionFactoryBean"
init-method="init" destroy-method="close">
<property name="poolSize" value="100" />
<property name="uniqueResourceName" value="esb" />
<property name="xaConnectionFactory" ref="customerOrderXAConnectionFactoryDestination" />
<property name="localTransactionMode" value="true" />
</bean>
<bean id="ddr" class="com.adeo.transverse.jms.forwarder.customerorder.DynamicDestinationResolver" />
<bean id="userCredentialsConnectionFactoryDestination"
class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter" lazy-init="true">
<property name="targetConnectionFactory">
<ref bean="connectionFactoryDestination" />
</property>
<property name="username" value="${destination.username}" />
<property name="password" value="${destination.passwd}" />
</bean>
Here's the integration part :
<!-- In bridge -->
<jms:message-driven-channel-adapter
id="StoreStockMotionSourceJmsAdapter" channel="bridgeChannelStoreStockMotionEnricher"
container="jmsContainerSourceStoreStockMotion" />
<!-- Channel -->
<si:channel id="bridgeChannelStoreStockMotionEnricher" />
<jms:outbound-channel-adapter id="StoreStockMotionDestinationJmsAdapter"
channel="bridgeChannelStoreStockMotionEnricher" jms-template="jmsTemplateStoreStockMotionDestination" />
<bean id="jmsTemplateStoreStockMotionDestination" class="org.springframework.jms.core.JmsTemplate">
<property name="transactionManager" ref ="transactionManager"/>
<property name="connectionFactory" ref="userCredentialsConnectionFactoryDestination" />
<property name="defaultDestinationName" value="${StoreStockMotion.destination.topic}" />
<property name="defaultDestination" ref="StoreStockMotionDestinationTopic" />
<property name="pubSubDomain" value="true"/>
</bean>
<!-- Topic JMS for published message -->
<bean id="StoreStockMotionDestinationTopic" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true">
<property name="jndiTemplate">
<ref bean="jndiTemplateDestination" />
</property>
<property name="jndiName">
<value>${StoreStockMotion.destination.topic}</value>
</property>
</bean>
<!-- Topic JMS for Subscribing Message -->
<bean id="jmsContainerSourceStoreStockMotion"
class="org.springframework.jms.listener.DefaultMessageListenerContainer"
lazy-init="true">
<property name="connectionFactory" ref="connectionFactorySource" />
<property name="destinationName" value="${StoreStockMotion.source.topic}" />
<property name="subscriptionDurable" value="true" />
<!-- 2 is client acknowledge -->
<property name="sessionAcknowledgeMode" value="2" />
<property name="durableSubscriptionName" value="${StoreStockMotion.source.subname}" />
<property name="sessionTransacted" value="false" />
<property name="pubSubDomain" value="true"/>
</bean>
Source and Destination are both encapsulated in XA connection factories and transactionManager handles the two transactions. Any idea what's missing ?

ActiveMQ Data persistent Issue in Java Spring

I have an application which uses embedded activeMQ 5.11. At the start of the application it creates activemq-data\producerBroker\KahaDB folder at the class path location. I do want to change the location but spring.xml doesn't take a location.
Spring.xml as given,
<bean id="producerBroker" class="org.apache.activemq.broker.SslBrokerService">
<property name="brokerName" value="producerBroker" />
<property name="persistent" value="true" />
<property name="persistenceAdapter" ref="persistenceAdapter"/>
<property name="transportConnectors">
<list>
<bean class="org.apache.activemq.broker.TransportConnector">
<property name="name" value="xxx"></property>
<property name="uri" value="${transportConnectorURIs}"></property>
</bean>
</list>
</property>
<property name="jmsBridgeConnectors">
<list>
<bean class="org.apache.activemq.network.jms.JmsQueueConnector">
<property name="outboundQueueConnectionFactory">
<bean class="org.apache.activemq.ActiveMQSslConnectionFactory">
<property name="brokerURL" value="${brokerURL}" />
<property name="userName" value="${username}" />
<property name="password" value="${password}" />
<property name="trustStore" value="${trust.store.path}" />
<property name="trustStorePassword" value="${trust.store.password}" />
<!-- <property name="keyStore" value="${key.store.path}"/> -->
<!-- <property name="keyStorePassword" value="${key.store.password}"/> -->
</bean>
</property>
<property name="outboundQueueBridges">
<list>
<bean class="org.apache.activemq.network.jms.OutboundQueueBridge">
<constructor-arg value="${screenshotQueueName}" />
</bean>
<bean class="org.apache.activemq.network.jms.OutboundQueueBridge">
<constructor-arg value="${resultXmlQueueName}" />
</bean>
</list>
</property>
</bean>
</list>
</property>
</bean>
<bean id="persistenceAdapter" class="org.apache.activemq.store.kahadaptor.KahaPersistenceAdapter">
<property name="directory" value="E:\test"/>
Current issue is it throws an error as "exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.activemq.store.kahadaptor.KahaPersistenceA
apter] for bean with name 'kahaPersistenceAdapter' defined in class path resource [spring/resultupload/resultupload.xml]; nested exception is java.la
g.ClassNotFoundException: org.apache.activemq.store.kahadaptor.KahaPersistenceAdapter"
Anyone has experience in the directory change in activeMQ 5.11 in java spring?
The destination of the persistence location must be defined at the broker level.
The kahaPersistenceAdapter (which was file based) was removed with version 5.9. You should use the kahaDB.
kahaDB - uses KahaDB an embedded lightweight non-relational database
<broker brokerName="broker" persistent="true" useShutdownHook="false">
<transportConnectors>
<transportConnector uri="tcp://localhost:61616"/>
</transportConnectors>
<persistenceAdapter>
<kahaDB directory="e:/temp" ... />
</persistenceAdapter>
</broker>
all valid attributes: http://activemq.apache.org/schema/core/activemq-core-5.11.0-schema.html#kahaDB

how to replace jdbctemplate with jpa in jobrepository of springbatch

hi actually springbatch jobrepository uses jdbctemplate for the CRUD operations of springbatch metadata
and my requirement is to replace jdbctemplate with jpa and do the crud operations...
the actual configuration of springbatch job repository is as follows...
can anyone kindly help regarding this issue...i searched alot but got nothing......
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3307/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- create job-meta tables automatically -->
<jdbc:initialize-database data-source="jobRepository-dataSource">
<jdbc:script location="classpath:/org/springframework/batch/core/schema-drop-mysql.sql" />
<jdbc:script location="classpath:/org/springframework/batch/core/schema-mysql.sql" />
</jdbc:initialize-database>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
lazy-init="true">
<property name="dataSource" ref="jobRepository-dataSource" />
</bean>
<bean id="jobRepository-transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
lazy-init="true">
<property name="dataSource" ref="jobRepository-dataSource" />
</bean>
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean" >
<property name="dataSource" ref="jobRepository-dataSource" />
<property name="transactionManager" ref="jobRepository-transactionManager" />
<property name="databaseType" value="mysql" />
<property name="isolationLevelForCreate" value="ISOLATION_DEFAULT" />
<property name="tablePrefix" value="batch_" />
</bean>
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
<bean id="jobExplorer"
class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean"
p:dataSource-ref="jobRepository-dataSource" p:tablePrefix="batch_" />
<bean id="jobRegistryBeanPostProcessor"
class="org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor">
<property name="jobRegistry" ref="jobRegistry" />
</bean>
<bean id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry" />
<bean id="jobOperator" class="org.springframework.batch.core.launch.support.SimpleJobOperator">
<property name="jobRepository" ref="jobRepository" />
<property name="jobLauncher" ref="jobLauncher" />
<property name="jobRegistry" ref="jobRegistry" />
<property name="jobExplorer" ref="jobExplorer" />
</bean>

JMS + Spring Integration + ActiveMQ ! Not working on JVM but working on activeMQ server

Create connection factory where broker url to JVM
<!--tcp://localhost:61616-->
<bean id="connectionFactoryActiveMQ" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="vm://localhost"/>
<property name="useAsyncSend" value="true"/>
</bean>
Create single connection factory becose need one connection
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="connectionFactoryActiveMQ"/>
</bean>
create topic destination because need implemented some classes
<bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">
<property name="physicalName" value="TEST"/>
</bean>
Generate jmsTemplate object
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="defaultDestination" ref="destination"/>
<property name="pubSubDomain" value="true"/>
</bean>
My classes who receive messages
<bean id="messageListener1" class="com.aimrposoft.jms.server.Server"/>
<bean id="messageListener2" class="com.aimrposoft.jms.server.Server1"/>
<bean id="messageListener3" class="com.aimrposoft.jms.server.Server2"/>
Generate message class
<bean id="producer" class="com.aimrposoft.jms.client.Producer"/>
<!--<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">-->
<!--<property name="connectionFactory" ref="connectionFactory"/>-->
<!--<property name="destination" ref="destination"/>-->
<!--<property name="messageListener" ref="messageListener1"/>-->
<!--</bean>-->
<jms:listener-container
connection-factory="connectionFactory" destination-type="topic" acknowledge="transacted">
<jms:listener destination="TEST" ref="messageListener1" method="onMessage" subscription="subscription"/>
<jms:listener destination="TEST" ref="messageListener2" method="onMessage" subscription="subscription"/>
<jms:listener destination="TEST" ref="messageListener3" method="onMessage" subscription="subscription"/>
</jms:listener-container>
When I am using vm://localhost message listener don't working correctly, but if I run activeMQ and change broker URL to tcp://localhost:61616, all work is fine.
I think you are missing configuration to start up a embedded broker, can you try adding this to your configuration also:
<amq:broker id="activeMQBroker">
<amq:transportConnectors>
<amq:transportConnector uri="vm://localhost" />
</amq:transportConnectors>
</amq:broker>
amq namespace prefix can be defined this way:
xmlns:amq="http://activemq.apache.org/schema/core"
To further Biju's answer, I use queues not topics, here's my spring config that appears to work just fine (62999 is just a random available port number):
<amq:broker useJmx="false" persistent="false">
<amq:transportConnectors>
<amq:transportConnector uri="tcp://localhost:62999" />
</amq:transportConnectors>
</amq:broker>
<bean id="rawConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="vm://localEmbeddedBroker" />
</bean>
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="rawConnectionFactory" />
<property name="sessionCacheSize" value="30" />
<property name="cacheProducers" value="true" />
<property name="cacheConsumers" value="false" />
</bean>

Categories