I'm using
Spring Batch
Step 1
Step 2 Master (Partitioner)
Step 3
Spring Integration (JMS) to communicate Master and Slave
The issue we are seeing is, the first slave handles all JMS messages instead of even distribution between slaves.
See configuration as below
Master
<bean id="PreProcess" class="com.job.tasklet.PreProcessTasklet" scope="step">
<constructor-arg index="0" value="${run.slave}"/>
<property name="maxNumberOfSlaves" value="#{jobParameters['max-slave-count']}"/>
</bean>
<bean id="PostProcess" class="com.job.tasklet.PostProcessTasklet" scope="prototype">
<constructor-arg index="0" ref="chpsJobDataSource"/>
</bean>
<bean id="partitioner" class="com.job.partition.DatabasePartitioner" scope="step">
<constructor-arg index="3" value="${max.row.count}"/>
</bean>
<bean id="partitionHandler" class="com.job.handler.StepExecutionAggregatorHandler">
<property name="stepName" value="processAutoHoldSlaveStep"/>
<property name="gridSize" value="${grid.size}"/>
<property name="replyChannel" ref="aggregatedGroupRuleReplyChannel"/>
<property name="messagingOperations">
<bean class="org.springframework.integration.core.MessagingTemplate">
<property name="defaultChannel" ref="groupRuleRequestsChannel"/>
</bean>
</property>
</bean>
<!-- Request Start -->
<int:channel id="groupRuleRequestsChannel" />
<int-jms:outbound-channel-adapter channel="groupRuleRequestsChannel" jms-template="jmsTemplateToSlave"/>
<bean id="jmsTemplateToSlave" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="receiveTimeout" value="5000"/>
<property name="defaultDestinationName" value="defaultRequest"/>
</bean>
<bean id="jmsTemplateFromSlave" class="org.springframework.jms.core.JmsTemplate" parent="jmsTemplateToSlave">
<property name="defaultDestinationName" value="defaultRequest"/>
</bean>
<!-- Response Test Start -->
<int:channel id="groupRuleReplyChannel">
<!-- <int:queue/> -->
</int:channel>
<int-jms:inbound-channel-adapter channel="groupRuleReplyChannel" jms-template="jmsTemplateFromSlave">
<int:poller id="defaultPoller" default="true" max-messages-per-poll="1" fixed-rate="3000" />
</int-jms:inbound-channel-adapter>
<!-- define aggregatedReplyChannel -->
<int:channel id="aggregatedGroupRuleReplyChannel">
<int:queue/>
</int:channel>
<int:aggregator ref="partitionHandler"
input-channel="groupRuleReplyChannel"
output-channel="aggregatedGroupRuleReplyChannel"
send-timeout="3600000"/>
Slave
<int:channel id="requestsChannel" />
<bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
<property name="brokerURL" value="${spring.activemq.broker-url}" />
<property name="trustAllPackages" value="true" />
</bean>
<int-jms:message-driven-channel-adapter id="jmsIn" destination-name="#{args[0]}" channel="requestsChannel" connection-factory="connectionFactory" max-messages-per-task="1"/>
<int:service-activator input-channel="requestsChannel" output-channel="replyChannel" ref="stepExecutionRequestHandler" />
<int:channel id="replyChannel" />
<int-jms:outbound-channel-adapter connection-factory="connectionFactory" destination-name="#{args[1]}" channel="replyChannel" />
Please advice if you have experience the issue.
Let me know if you need more information.
Note: I already search a lot at here and google but no luck for solution yet.
ActiveMQ uses a prefetch of 1000 by default see here.
In other words, the first (up to) 1000 partitions will go to the first consumer etc.
You can reduce the prefetch; 1 is probably fine for this application.
Related
We are using Spring Integration to consume messages from a queue and the requirement is to send the message to Error queue if there is any issue in the processing of the message consumed.
Flow works fine but one issue we see is when there is any exception thrown in processing of message, the message is redirected to the Error queue which we have configured but it is appended by the entire stack trace of the exception.
We are looking only for the original message to be delivered to the queue. Below is the configuration we have done,
<bean id="errorQ" class="com.ibm.mq.jms.MQQueue">
<constructor-arg value="${error.queue}" />
</bean>
<bean id="inQ" class="com.ibm.mq.jms.MQQueue">
<constructor-arg value="${inbound.queue}" />
</bean>
<int:channel id="error" />
<int:channel id="inbound" />
<int-jms:message-driven-channel-adapter
id="jmsIn" channel="inbound" container="messageListenerContainer"
acknowledge="transacted"></int-jms:message-driven-channel-adapter>
<int-jms:outbound-channel-adapter id="jmsError"
channel="error" connection-factory="mqConnectionFactory"
destination="errorQ" delivery-persistent="true"></int-jms:outbound-channel-adapter>
<int:service-activator id="service"
input-channel="inbound" ref="messageListener" method="someMethodInListerner">
<int:request-handler-advice-chain>
<ref bean="retryWithBackoffAdviceSession" />
</int:request-handler-advice-chain>
<bean id="retryWithBackoffAdviceSession"
class="org.springframework.integration.handler.advice.RequestHandlerRetryAdvice">
<property name="retryTemplate">
<bean class="org.springframework.retry.support.RetryTemplate">
<property name="backOffPolicy">
<bean class="org.springframework.retry.backoff.ExponentialBackOffPolicy">
<property name="initialInterval" value="${initialInterval}" />
<property name="multiplier" value="${multiplier}" />
<property name="maxInterval" value="${maxInterval}" />
</bean>
</property>
<property name="retryPolicy">
<bean class="org.springframework.retry.policy.SimpleRetryPolicy">
<property name="maxAttempts" value="${redelivery}" />
</bean>
</property>
</bean>
</property>
<property name="recoveryCallback">
<bean
class="org.springframework.integration.handler.advice.ErrorMessageSendingRecoverer">
<constructor-arg ref="error" />
</bean>
</property>
</bean>
The message sent to the error is an ErrorMessage with payload MessagingException.
The MessagingException has two properties cause and failedMessage.
So, if you want to send the original failed message to errorQ, you will need to add a transformer to the error flow...
<int:transformer ... expression="payload.failedMessage" />
EDIT
<int:chain input-channel="error">
<int:transformer expression="payload.failedMessage" />
<int-jms:outbound-channel-adapter ... />
</int:chain>
EDIT2
Generally, when using these techniques, it's useful to convey the reason for the failure. You can add the exception's message as a header...
<int:chain input-channel="error">
<int:header-enricher>
<int:header name="failureReason" expression="payload.cause.message" />
</int:header-enricher>
<int:transformer expression="payload.failedMessage" />
<int-jms:outbound-channel-adapter ... />
</int:chain>
I have a activemq jdbcPersistenceAdapter configured to use Oracle. The primary broker gets a lock, but when I send messages to the queue, I cannot see them in a database table. I do not want the the messages cached in any way, since they will not be consumed until there are no more producers (long story).
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="primary" useJmx="true" useShutdownHook="false">
<managementContext>
<!-- use appserver provided context instead of creating one,
for jboss use: -Djboss.platform.mbeanserver -->
<managementContext createConnector="false"/>
</managementContext>
<persistenceAdapter>
<jdbcPersistenceAdapter dataSource="#oracle-ds" createTablesOnStartup="false" lockKeepAlivePeriod="30000"/>
</persistenceAdapter>
<transportConnectors>
<transportConnector uri="tcp://localhost:61616"/>
</transportConnectors>
<!-- Spring JMS Producer Configuration -->
<bean id="jmsProducerTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="defaultDestination" ref="p3dPausedTransactionDestination"/>
<property name="deliveryPersistent" value="true"/>
</bean>
<!-- Message Listener Container -->
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destination" ref="p3dPausedTransactionDestination"/>
<property name="messageListener" ref="transactionMessageConsumer" />
<property name="sessionTransacted" value="true"/>
<property name="autoStartup" value="true"/>
<property name="sessionAcknowledgeModeName" value="AUTO_ACKNOWLEDGE"/>
</bean>
<!-- JMS Factory -->
<beans profile="local, test, dev, dev2, sit, uat, tt, prod">
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory">
<bean class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:jboss/activemq/p3dConnectionFactory"/>
<property name="lookupOnStartup" value="true"/>
</bean>
</property>
</bean>
</beans>
Once I added the second broker, the messages started appearing in the database.
I am implementing Spring MDP + JMSTemplate to send and receive the message. The message send mechanism is working fine, however the MDP is not getting invoked. I tried testing the via plain receiver, and was able to retrieve the message, but not via MDP. What could be the problem? I can see the messages getting accumulated in the request queue, but somehow the MDP is not getting trigger. Am I missing anything here in configurations or something else needs to be taken care of?
Here's the Spring Config. The Java class to send and received are pretty much standard ones.
<bean id="cookieRequestListener" class="com.text.jms.mq.mdp.RequestQueueMDP">
<property name="logger" ref="mqLogger" />
<property name="scoringEngine" ref="scoringEngine" />
<property name="mqSender" ref="jmsMQSender" />
</bean>
<bean id="CookieRequestContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="cachedConnectionFactory" />
<property name="destination" ref="jmsRequestQueue" />
<property name="messageListener" ref="cookieRequestListener" />
</bean>
<bean id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="jndiTemplate" />
</property>
<property name="jndiName">
<value>java:/jms/queueCF</value>
</property>
</bean>
<!-- A cached connection to wrap the Queue connection -->
<bean id="cachedConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="jmsConnectionFactory"/>
<property name="sessionCacheSize" value="10" />
</bean>
<!-- jms Request Queue Configuration -->
<bean id="jmsRequestQueue" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="jndiTemplate" />
</property>
<property name="jndiName">
<value>java:/jms/cookieReqQ</value>
</property>
</bean>
<!-- jms Response Queue Configuration -->
<bean id="jmsResponseQueue" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="jndiTemplate" />
</property>
<property name="jndiName">
<value>java:/jms/cookieResQ</value>
</property>
</bean>
<bean id="jmsJMSTemplate" class="org.springframework.jms.core.JmsTemplate" >
<property name="connectionFactory" ref="cachedConnectionFactory" />
</bean>
<!-- jms MQ Utility -->
<bean id="jmsMQSender" class="com.text.jms.util.MQSender">
<property name="jmsTemplate">
<ref bean="jmsJMSTemplate"></ref>
</property>
<property name="defaultDestination">
<ref bean="jmsRequestQueue" />
</property>
<property name="logger" ref="mqLogger" />
</bean>
I have two different instances of RabbitMQ at localhost:5672 and localhost:5676.
Also I configured it in my application context:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation=
"http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
xmlns:cache="http://www.springframework.org/schema/cache">
<bean id="rabbitConnectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
<property name="username" value="${rabbit.username}"/>
<property name="password" value="${rabbit.password}"/>
<property name="host" value="${rabbit.host}"/>
<property name="port" value="${rabbit.port}"/>
</bean>
<bean id="amqpTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate">
<property name="connectionFactory" ref="rabbitConnectionFactory"/>
<property name="exchange" value="${rabbit.exchange}"/>
<property name="routingKey" value="${rabbit.routing-key}"/>
<property name="queue" value="${rabbit.queue}"/>
</bean>
<bean id="admin" class="org.springframework.amqp.rabbit.core.RabbitAdmin">
<constructor-arg ref="rabbitConnectionFactory" />
</bean>
<bean id="rabbitTxManager"
class="org.springframework.amqp.rabbit.transaction.RabbitTransactionManager">
<property name="connectionFactory" ref="rabbitConnectionFactory"/>
</bean>
<bean id="messageListenerContainer" class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
<property name="channelTransacted" value="true"/>
<property name="transactionManager" ref="rabbitTxManager"/>
<property name="prefetchCount" value="${rabbit.prefetchCount}"/>
<property name="txSize" value="${rabbit.txSize}"/>
<property name="connectionFactory" ref="rabbitConnectionFactory"/>
<property name="messageListener" ref="handler"/>
<property name="queueNames" value="${rabbit.queue}"/>
<property name="concurrentConsumers" value="${rabbit.concurrentConsumers}"/>
<property name="errorHandler" ref="errorHandler"/>
</bean>
<bean id="handler" class="com.pb.pav.timingbus.amqp.MessageHandler" >
<constructor-arg index="0" ref="gson"/>
<constructor-arg index="1" ref="sender"/>
</bean>
<bean id="errorHandler" class="com.pb.pav.timingbus.amqp.ExceptionHandler"/>
<bean id="gson" factory-bean="formattedBuilder" factory-method="create" />
<bean id="gsonBuilder" class="com.google.gson.GsonBuilder" />
<bean id="formattedBuilder" factory-bean="gsonBuilder" factory-method="setDateFormat">
<constructor-arg index="0" value="yyyy-MM-dd HH:mm:ss.SSS"/>
</bean>
<bean id="sender" class="com.pb.timing.client.TimingWSClient">
<constructor-arg index="0" value="${timing.url}"/>
</bean>
<bean id="busService" class="com.pb.pav.timingbus.BusService" />
<bean id="answerConnFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
<property name="username" value="guest"/>
<property name="password" value="guest"/>
<property name="host" value="localhost"/>
<property name="port" value="5676"/>
<property name="channelCacheSize" value="2"/>
</bean>
<bean id="rabbitAnswer" class="org.springframework.amqp.rabbit.core.RabbitTemplate">
<property name="connectionFactory" ref="answerConnFactory"/>
<property name="exchange" value="timinganswers"/>
<property name="routingKey" value="temp"/>
<property name="queue" value="temp"/>
</bean>
<bean id="answerAdmin" class="org.springframework.amqp.rabbit.core.RabbitAdmin">
<constructor-arg ref="answerConnFactory" />
</bean>
<bean id="mesSender" class="com.pb.pav.timingbus.amqp.MessageSender" >
<constructor-arg index="0" ref="gson"/>
<constructor-arg index="1" ref="rabbitAnswer"/>
</bean>
My spring web application produce messages to rabbit(localhost:5672, id="amqpTemplate") and then consume from queue and save to DB.
After saving to DB application produce answers (ok or errors) and I need publish answers to the second rabbitMQ(localhost:5676, id="rabbitAnswer").
When I try publish answers to second rabbit(localhost:5676, id="rabbitAnswer") they published to first rabbit, and I dont understand why??? I am logging port for connection before send messages and it right - 5676, but application send messages to 5672.
I suspect that problem is in channels, because at the second rabbit I didn't see any opened channels. So, how I can declare different channels to my second rabbit and send messages in the right way?
I set up a new project with Tomcat 7.0 and an embedded HornetQ JMS server.
I used these 2 tutorials to help me:
http://www.javacodegeeks.com/2010/06/spring-3-hornetq-21-integration.html
http://wash-inside-out.blogspot.com/2010/08/hornetq-jms-integration-with-tomcat.html
But as it is mentioned in the tutos, the Tomcat JNDI repository is readonly (cannot find a way to write) and I configured a "separated" JNDI used by HornetQ, the messaging works, but Tomcat cannot access it.
Normally, in my other projects using Tomcat, I defined the datasource as a global resource in the server.xml and I map it in the context.xml. doing this, the definition of the datasource (jdbc url, credentials, etc...) are outside the application and can be managed by environment (dev, test, prod, ...) but I cannot find a way to do it with the other JNDI.
Currently, the datasource is defined in my application with an external property file for the parameters but I am not really satisfied with this solution.
Here is my Spring configuration:
<!-- enable autowire -->
<context:annotation-config />
<!-- enable transaction demarcation with annotations -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource">
<property name="driverClassName" value="com.informix.jdbc.IfxDriver" />
<property name="url" value="${URL}" />
<property name="username" value="${user}" />
<property name="password" value="${password}" />
<property name="maxActive" value="50" />
<property name="maxIdle" value="10" />
<property name="maxWait" value="1000" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="300" />
<property name="logAbandoned" value="true" />
</bean>
<!-- HornetQ config -->
<bean name="namingServerImpl" class="org.jnp.server.NamingBeanImpl" init-method= "start" destroy-method="stop" >
<!-- configure HornetQ JNDI server not to use an existing JNDI service if available -->
<property name="useGlobalService" value="false" />
</bean>
<bean name="namingServer" class="org.jnp.server.Main" init-method="start" destroy-method="stop">
<property name="namingInfo" ref="namingServerImpl" />
<property name="port" value="1099" />
<property name="bindAddress" value="localhost" />
<property name="rmiPort" value="1098" />
<property name="rmiBindAddress" value="localhost" />
</bean>
<bean name="mbeanServer" class="java.lang.management.ManagementFactory" factory-method="getPlatformMBeanServer" />
<bean name="fileConfiguration" class="org.hornetq.core.config.impl.FileConfiguration"
init-method="start" destroy-method="stop" />
<bean name="hornetQSecurityManagerImpl" class="org.hornetq.spi.core.security.HornetQSecurityManagerImpl" />
<!-- The core server -->
<bean name="hornetQServerImpl" class="org.hornetq.core.server.impl.HornetQServerImpl">
<constructor-arg index="0" ref="fileConfiguration" />
<constructor-arg index="1" ref="mbeanServer" />
<constructor-arg index="2" ref="hornetQSecurityManagerImpl" />
</bean>
<!-- The JMS server -->
<bean name="jmsServerManagerImpl" class="org.hornetq.jms.server.impl.JMSServerManagerImpl"
init-method="start" destroy-method="stop" depends-on="namingServer">
<constructor-arg ref="hornetQServerImpl" />
</bean>
<!-- to use HornetQ messaging service through Spring we can either create a connection factory, or lookup one from JNDI -->
<bean name="connectionFactory" class="org.hornetq.jms.client.HornetQConnectionFactory">
<constructor-arg index="0" type="boolean" value="false"/>
<constructor-arg index="1">
<bean class="org.hornetq.api.core.TransportConfiguration">
<constructor-arg index="0" type="java.lang.String" value="org.hornetq.integration.transports.netty.NettyConnectorFactory" />
<constructor-arg index="1">
<map key-type="java.lang.String" value-type="java.lang.Object">
<entry key="port" value="5445"></entry>
</map>
</constructor-arg>
</bean>
</constructor-arg>
</bean>
<bean id="notificationsQueue" class="org.springframework.jndi.JndiObjectFactoryBean" depends-on="jmsServerManagerImpl">
<property name="jndiName">
<value>/queue/Notifications</value>
</property>
</bean>
<bean id="inVMConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean" depends-on="jmsServerManagerImpl">
<property name="jndiName">
<value>/ConnectionFactory</value>
</property>
</bean>
How can I managed it in a better way, I mean, define the datasource on the server side as usual? Is there a configuration to tell Tomcat to use the external JNDI I defined or create a read/write repo?