Spring polling + Hibernate

时间:2015-11-07 05:09:25

标签: java spring-integration

场景:每隔'n'秒轮询数据库并检索列表。做一些内部业务验证。如果验证只是成功,则将检索到的列表发送到外部系统。接收来自该系统的确认响应(收到此消息后,轮询器应发送下一个列表)。然后执行一些业务操作,如调用其他系统不需要轮询。 任何人都可以告诉我如何处理这种情况?

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");
    }

     
}

我添加了一个直接通道来轮询DB。还添加了一个执行器通道..但是我得到了异常

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)

任何人都可以帮忙解决这个问题......我只创建了 PollerService.java 并且没有任何想法来实现执行者渠道和任务执行者

1 个答案:

答案 0 :(得分:0)

如果您使用<poller>选项并且您的下游流程是单线程(仅fixed-delay s),则DirectChannel默认为您执行此操作。在这种情况下,下一个轮询任务(在您的情况下使用JPA读取数据库)将不会在前一个完成之前启动,有时会像fixed-delay那样启动。见PeriodicTrigger,BTW。

关于您的“然后执行一些业务操作”。您应该在“接收确认响应”之后将下一个通道设置为ExecutorChannel,以释放轮询线程以进行下一次轮询。

您应该在Spring Integration Reference Manual中阅读更多内容,尤其是Poller