Jill
Jill

Reputation: 333

Spring polling + Hibernate

Scenario : poll the database every 'n' seconds and retrieve list. Do some internal business validations.If validations are success only, send the retrieved list to an external system.Receive an acknowledgment response from that system(After receiving this,poller should send next list).Then perform some business operations like calling other systems where polling is not needed. can any one tell me how this scenario can be handled?

poller.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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:task="http://www.springframework.org/schema/task" xmlns:int-http="http://www.springframework.org/schema/integration/http"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
            http://www.springframework.org/schema/integration
            http://www.springframework.org/schema/integration/spring-integration-4.1.xsd
               http://www.springframework.org/schema/integration/http
    http://www.springframework.org/schema/integration/http/spring-integration-http-4.1.xsd
            http://www.springframework.org/schema/integration/jdbc
            http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc-4.1.xsd
            http://www.springframework.org/schema/jdbc
            http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd">

	<import resource="persistence-config.xml" />

	<int:channel id="inchannel">
	</int:channel>

	<int:channel id="outchannel">
	<int:dispatcher task-executor="taskExecutor"/> 
		</int:channel>
		
	<task:executor id="taskExecutor" pool-size="2"/>


	<bean id="poller" class="main.java.com.as.poller.PollerService" />


	<int:service-activator input-channel="inchannel"
		output-channel="outchannel" ref="poller" method="sendMessage" />


	<int-jdbc:inbound-channel-adapter
		query="select loyalty_id from TBL_RECEIPT where  receipt_status=0"
		channel="inchannel" data-source="dataSource" max-rows-per-poll="1">
		<int:poller fixed-rate="5000">
		</int:poller>
	</int-jdbc:inbound-channel-adapter>


</beans>

ackage main.java.com.as.poller;
 

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.stereotype.Component;


 @Component
public class PollerService{
    public void sendMessage()
    {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                "AS-servlet.xml");
    	DirectChannel directChannel = (DirectChannel) context.getBean("inchannel");
    }

     
}

I added a direct channel to poll the DB.Also added an executer channel..But i am getting the exception

Error creating bean with name 'org.springframework.integration.config.ServiceActivatorFactoryBean#0': Cannot resolve reference to bean 'executerChannel' while setting bean property 'outputChannel'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'executerChannel' is defined
rg.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1477)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1222)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)

Can anyone please help with this...I have created only PollerService.java and dont have any idea to implement executer channel and task executer

Upvotes: 1

Views: 1788

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121262

The <poller> does that for you by default if you use fixed-delay option and your downstream flow is single threaded (only DirectChannels). In that case the next polling task (in your case reading DB using JPA) won't be started until the finish of previous one and some time like fixed-delay. See PeriodicTrigger, BTW.

Regarding your "Then perform some business operations". You should just make the next channel after "Receive an acknowledgment response" as an ExecutorChannel to free the polling Thread for the next poll.

You should really read more in the Spring Integration Reference Manual and especially about the Poller.

Upvotes: 1

Related Questions