logging the messages of queue to a text file - spring

I have an below spring integration xml in which I send an message to queue and that message is consumed finally and displayed on console but now I want to customize it lets say those message which are consumed should be finally written (in other words those messages need to be logged)in a text file and that text file should be saved in C: drive of my folder the name of the file is messageslog.txt
Please advise how can I add such functionality in spring integration itself to acheieve this functionality I have come to know that in spring integration something like file outbound channel adapter will help
below is my spring integtaion xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
xmlns:jms="http://www.springframework.org/schema/integration/jms"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms-2.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<int:poller id="poller" default="true">
<int:interval-trigger interval="200" />
</int:poller>
<int:channel id="output">
<int:queue capacity="10" />
</int:channel>
<bean id="tibcoEMSJndiTemplate" 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://labc.net:7033</prop>
<prop key="java.naming.security.principal">wer</prop>
<prop key="java.naming.security.credentials">wer</prop>
</props>
</property>
</bean>
<bean id="tibcoEMSConnFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="tibcoEMSJndiTemplate" />
</property>
<property name="jndiName">
<value>GenericConnectionFactory</value>
</property>
</bean>
<bean id="tibcosendJMSTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref local="tibcoEMSConnFactory" />
</property>
<property name="defaultDestinationName">
<value>test.data</value>
</property>
<property name="pubSubDomain">
<value>false</value>
</property>
<property name="receiveTimeout">
<value>120000</value>
</property>
</bean>
<int:channel id="input">
</int:channel>
<jms:outbound-channel-adapter channel="input"
destination-name="test.data" connection-factory="tibcoEMSConnFactory" />
<jms:message-driven-channel-adapter
channel="output" destination-name="test.data" connection-factory="tibcoEMSConnFactory" />
</beans>

Change input to a <publish-subscribe-channel/>
set order="1" on the jms outbound channel adapter
subscribe a file outbound channel adapter (mode=APPEND) with order="2"
By default, the file adapter will be invoked only if the send to JMS is successful.
You don't seem to have a consumer on output; however, to avoid message loss, output should NOT be a QueueChannel, just remove the <queue/> element (and you don't need a poller).

Related

Connect to WebSphere MQ from Spring using Spring configuration

I am using Spring DSL of Apache Camel in our project which is a Spring Boot application. We are connecting to various MQs like Apache Active MQ, Active MQ Artemis, IBM MQ in our project to send and receive message. We are able to successfully connect to Apache Active MQ and Active MQ Artemis using the below configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">org.wildfly.naming.client.WildFlyInitialContextFactory</prop>
<prop key="java.naming.provider.url">${provider.url}</prop>
<prop key="java.naming.security.principal">${security.principal}</prop>
<prop key="java.naming.security.credentials">${security.credentials}</prop>
</props>
</property>
</bean>
<bean id="jmsQueueConnectionFactoryT24" class="org.springframework.jndi.JndiObjectFactoryBean" primary="true">
<property name="jndiTemplate">
<ref bean="jndiTemplate" />
</property>
<property name="jndiName">
<value>${connectionFactory}</value>
</property>
</bean>
<bean name="jmsT24" class="org.apache.camel.component.jms.JmsComponent">
<property name="connectionFactory" ref="jmsQueueConnectionFactory" />
<property name="concurrentConsumers" value="${jboss.jms.concurrentConsumers}" />
</bean>
</beans>
I am now trying to figure out how to connect to IBM MQ and what all parameters to pass to create the connection factory like how to pass queue manager and channel information but unable to do so as it is not working, can anyone please provide what would be the InitialContextFactory and how to pass parameters for creating connection factory?

How to read application.properties file , while starting Ignite from spring config file

I was trying to start a Apache Ignite from spring config xml file. The spring xml has keys that it reads from application.properties file.
I am using the following command to start Apache Ignite
apache-ignite-fabric-2.6.0-bin\bin\ignite.bat D:\workspace\<application-name>\src\main\resources\ignite.xml
I tried setting up a CLASSPATH environment variable where i have kept all the related spring and ignite jars and have also kept the application.properties file. Still its somehow now able to read the apploication.properties file.
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'ignite.cfg' defined in URL [file:/D:/workspace/<application-name>/src/main/resources/ignite.xml]: No configuration setting found for key 'zookeeper'; nested exception is com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'zookeeper'
at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:223)
at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:222)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:86)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:283)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:163)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:687)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:525)
at org.apache.ignite.internal.util.spring.IgniteSpringHelperImpl.applicationContext(IgniteSpringHelperImpl.java:381)
I have tried setting the classpath as well as tried keeping the file/jar in lib directory of IGNITE_HOME directory.
ignite.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
<!--<property name="gridLogger">
<bean class="org.apache.ignite.logger.slf4j.Slf4jLogger"/>
</property>-->
<!-- Enabling Apache Ignite native persistence. -->
<property name="peerClassLoadingEnabled" value="false"/>
<property name="dataStorageConfiguration">
<bean class="org.apache.ignite.configuration.DataStorageConfiguration">
<property name="walMode" value="NONE"/>
<property name="walArchivePath" value="D:\work\TIWorkspace\ignite\wal\archive"/>
<property name="defaultDataRegionConfiguration">
<bean class="org.apache.ignite.configuration.DataRegionConfiguration">
<property name="persistenceEnabled" value="true"/>
<!-- Increasing the buffer size to 1 GB. -->
<property name="checkpointPageBufferSize"
value="#{1024L * 1024 * 1024}"/>
</bean>
</property>
</bean>
</property>
<property name="binaryConfiguration">
<bean class="org.apache.ignite.configuration.BinaryConfiguration">
<property name="compactFooter" value="false"/>
<property name="idMapper">
<bean class="org.apache.ignite.binary.BinaryBasicIdMapper">
<property name="lowerCase" value="true"/>
</bean>
</property>
<property name="nameMapper">
<bean class="org.apache.ignite.binary.BinaryBasicNameMapper">
<property name="simpleName" value="true"/>
</bean>
</property>
</bean>
</property>
<property name="discoverySpi">
<bean class="org.apache.ignite.spi.discovery.zk.ZookeeperDiscoverySpi">
<property name="zkConnectionString" value="localhost:2181"/>
<!--<property name="zkConnectionString" value="${zookeeper.server}"/>-->
<property name="zkRootPath" value="/dbobjects"/>
</bean>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:application.properties</value>
</property>
</bean>
Any leads could be helpful.
Thanks in advance.
The most straightforward way to achieve that is to use file-based resource URL. This one worked for me:
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:${IGNITE_HOME}/libs/application.properties</value>
</list>
</property>
</bean>

Weblogic Deployment Unable to Resolve Local Queue

We have a spring-based application that is failing to deploy on a Weblogic 10.3 container. Upon deployment, the application attempts to look up two local JMS queues within the Weblogic container's JMS module and, when the deployment takes place, the application locates one local queue okay but not the other.
Both queues are configured exactly the same except for their names are different. Why does the application locate one queue okay but not the other???
I've checked queue names JNDI names many times over and I can't see any spelling errors or anything like that.
I have turned trace logging on and I can see that the connection factory used to look up both queues is the same, the spring JMS configuration is exactly the same for both queues yet one it finds the other it does not.
I don't know what else to check to establish what the problem might be... any ideas?
This is the error I get when it fails to look up one of the queues in the Weblogic JNDI tree:
Caused by: javax.naming.NameNotFoundException: Unable to resolve 'QUEUE_NAME'. Resolved ''; remaining name 'QUEUE_NAME'
at weblogic.jndi.internal.BasicNamingNode.newNameNotFoundException(BasicNamingNode.java:1139)
PS: Both queues have the same Subdeployment and same Targets configured.
---- edited to add artifact's Spring XML Configuration snippet below ----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:security="http://www.springframework.org/schema/security"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang-3.0.xsd">
<context:component-scan base-package="com.company.service" />
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
</props>
</property>
</bean>
<!-- this is the Message Driven POJO (MDP) -->
<bean id="messageListener" class="com.company.service.controller.ServiceJMSListener" />
<!-- this is the message listener container -->
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="queueConnectionFactory" />
<property name="destination" ref="inboundQueue" />
<property name="messageListener" ref="messageListener" />
<property name="concurrentConsumers" value="1" />
</bean>
<!-- JNDI Connection Factory -->
<bean id="queueConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="jndiTemplate" />
</property>
<property name="jndiName">
<value>SERVICE_QCF</value>
</property>
</bean>
<!-- Queue to listen to -->
<bean id="inboundQueue" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="jndiTemplate" />
</property>
<property name="jndiName">
<value>QUEUE_A</value>
</property>
</bean>
<bean id="outboundQueue" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="jndiTemplate" />
</property>
<property name="jndiName">
<value>QUEUE_B</value>
</property>
</bean>
<bean id="queueTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref bean="queueConnectionFactory" />
</property>
<property name="destinationResolver">
<ref bean="jmsDestinationResolver" />
</property>
</bean>
<bean id="jmsDestinationResolver"
class="org.springframework.jms.support.destination.JndiDestinationResolver">
<property name="jndiTemplate">
<ref bean="jndiTemplate" />
</property>
<property name="cache">
<value>true</value>
</property>
</bean>
</beans>
Can you have a look to the JDNI tree in your running server to check if the queue is created or not, and to which jndi name it is bound to ? Use the admin console for this purpose.

Spring MessageListener not invoked in Tomcat Server when trying to read a Foriegn Weblogic JMS Topic

I have deployed an application in Tomcat7 to read messages from Foreign JMS Topic in Weblogic 11g. This is my spring application context xml. I have included wlthint3client.jar in my classpath.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Uncomment and add your base-package here: <context:component-scan base-package="org.springframework.samples.service"/> -->
<tx:jta-transaction-manager/>
<bean id="jmsTransactionManager" class="org.springframework.jms.connection.JmsTransactionManager" p:connectionFactory-ref="clarifyconnectionFactory"/>
<bean id="clarifyTopic" class="org.springframework.jndi.JndiObjectFactoryBean"
p:jndiName="jms/ATomcatClarifyTopic"
p:proxyInterface="javax.jms.Destination"
p:jndiTemplate-ref="jndiTemplate"/>
<bean id="messageAdapter" class="org.springframework.jms.listener.adapter.MessageListenerAdapter" p:defaultListenerMethod="receive">
<constructor-arg>
<bean class="com.jnpr.clarify.jms.DefaultTextMessageDelegate"/>
</constructor-arg>
</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://weblogichost:8001</prop>
</props>
</property>
</bean>
<!-- Lookup JMS Connection Factory -->
<bean id="clarifyconnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"
p:jndiName="jms/TestTopicConnectionFactory" p:proxyInterface="javax.jms.TopicConnectionFactory"
p:jndiTemplate-ref="jndiTemplate"
p:exposeAccessContext="true"/>
<!-- Configuring JMS Template -->
<bean id='jmsTemplate' class="org.springframework.jms.core.JmsTemplate"
c:_0-ref="clarifyconnectionFactory" />
<!-- Configuring Topic Listener -->
<bean id="topicListener" class="com.jnpr.clarify.jms.TopicListener" />
<!-- Configuring MessageListenerContainer -->
<bean id="JMSMessageListenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer"
p:destination-ref="clarifyTopic" p:connectionFactory-ref="clarifyconnectionFactory"
p:messageListener-ref="messageAdapter" p:transactionManager-ref="jmsTransactionManager"
p:durableSubscriptionName="TomcatClarify"/>
</beans>
In this i have used MessageListenerAdapter as JMS Listener. I have
also tried witha a bean implementing javax.jms.MessageListener. But
still getting same exception.
I am not using any transaction management in my code but still configured Transaction manager to check if that fixes the issue.
Am getting below exception in tomcat server log.
WARNING: Setup of JMS message listener invoker failed for destination 'JUN_JMS_SOA_Module!ATomcatClarifyTopic' - trying to recover. Cause: [JMSClientExceptions:055142]Foreign destination, JMS_SOA_Module!TomcatClarifyTopic
Jan 01, 2015 7:09:13 PM org.springframework.jms.listener.DefaultMessageListenerContainer refreshConnectionUntilSuccessful
INFO: Successfully refreshed JMS Connection
Please help if am doing anything wrong.
The above code working when message listener container configured using jms namespace as below instead of regular spring bean.
<jms:listener-container container-type="default" acknowledge="auto"
connection-factory="clarifyconnectionFactory" destination-type="durableTopic"
destination-resolver="jmsDestinationResolver">
<jms:listener destination="jms/ATomcatClarifyTopic" ref="topicListener"/>
</jms:listener-container>
<bean id="jmsDestinationResolver"
class="org.springframework.jms.support.destination.JndiDestinationResolver">
<property name="jndiTemplate">
<ref bean="jndiTemplate" />
</property>
<property name="cache">
<value>true</value>
</property>
</bean>

Spring Quartz simple trigger not firing as expected

I am having a simple trigger bean which should be fired for every 20min.
For that I am specifying the repeatinterval value in properties file. But my job is getting waked up every minute instead of every 20min.
sample xml
<bean id="propertyLoaderJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="propertyloader" />
<property name="targetMethod" value="propFlagValidator" />
<property name="concurrent" value="false" />
</bean>
<bean id="propertyLoaderTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="propertyLoaderJob" />
<property name="repeatInterval" value="${quartz.scheduler.repeatInterval}" />
<property name="startDelay" value="${quartz.scheduler.startDelay}" />
</bean>
in the properties file I have these fields
quartz.scheduler.repeatInterval=1200000
quartz.scheduler.startDelay=1000
What could be the possbile reason for this?
Please help.
Thanks in advance.
Have you specified a property placeholder for this,
Using Context namespace,
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
. . .
<context:property-placeholder location="quartz.scheduler.properties" />
. . .
</beans>
or Adding PropertyPlaceholderConfigurer bean in application context
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:quartz.scheduler.properties</value>
</property>
</bean>
Are you sure that you are not using any other trigger for the same class/job i.e. propertyLoaderJob. It may have happened that you are using a cron expression as well associated with the same job. It will be great if you can share the complete spring xml file.
For future reference i am answering.
Along with the mentioned part in my question i forgot to add the below code. That is for actually triggering it:
<bean name="nonclusterMode" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="propertyLoaderTrigger" />
</list>
</property>
</bean>
Now it is working as expected.

Resources