2015-11-07 201 views
1

場景:每'n'秒輪詢數據庫並檢索列表。做一些內部業務驗證。如果驗證只是成功的,則將檢索到的列表發送到外部系統。接收來自該系統的確認響應(接收到該請求後,輪詢器應發送下一個列表)。然後執行一些業務操作,如調用其他系統不需要輪詢。 任何人都可以告訴我這種情況如何處理?春季輪詢+休眠

poller.xml

<?xml version="1.0" encoding="UTF-8"?> 
 
<beans xmlns="http://www.springframework.org/schema/beans" 
 
\t xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration" 
 
\t xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc" 
 
\t xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 
 
\t xmlns:task="http://www.springframework.org/schema/task" xmlns:int-http="http://www.springframework.org/schema/integration/http" 
 
\t 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"> 
 

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

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

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

 

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

 

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

 

 
\t <int-jdbc:inbound-channel-adapter 
 
\t \t query="select loyalty_id from TBL_RECEIPT where receipt_status=0" 
 
\t \t channel="inchannel" data-source="dataSource" max-rows-per-poll="1"> 
 
\t \t <int:poller fixed-rate="5000"> 
 
\t \t </int:poller> 
 
\t </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"); 
 
    \t DirectChannel directChannel = (DirectChannel) context.getBean("inchannel"); 
 
    } 
 

 
     
 
}

我添加了一個直接通道輪詢DB.Also增加了一個執行者channel..But我得到例外

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) 
 
\t at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) 
 
\t at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1477) 
 
\t at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1222) 
 
\t at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 
 
\t at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)

誰能請幫助這個......我只創建了PollerService.java和不要有任何想法來實現執行器通道和任務執行者

+0

這個問題實在太寬泛;閱讀[Spring集成參考手冊](http://docs.spring.io/spring-integration/reference/html/)。當你瞭解基本概念時;閱讀JPA支持部分。有[很多示例](https://github.com/spring-projects/spring-integration-samples),至少包括一個使用JPA和hibernate的示例。當你做完所有事情後,如果你有特定的問題,請回到這裏。 –

回答

0

<poller>做對於默認情況下,如果您使用fixed-delay選項,並且您的下游流爲單線程(僅限於DirectChannel s)。在這種情況下,下一個輪詢任務(在使用JPA讀取數據庫的情況下)將不會在上一個輪詢任務完成之前開始,並且會像fixed-delay那樣。參見PeriodicTrigger,順便說一下。

關於您的「然後執行一些業務操作」。您應該在「接收確認響應」之後將下一個頻道設置爲ExecutorChannel,以釋放下一次輪詢的輪詢線程。

您應該在Spring集成參考手冊中詳細閱讀更多內容,特別是關於Poller

+0

..我可以得到一個可以處理我的場景的示例java代碼片段.. – Jill

+0

???不知道你有什麼問題,但我們無法幫助你編碼,你應該向我們展示一些PoC,並指出卡住的位置。否則,它聽起來像:請爲我寫下申請! –

+0

好吧,我說過它是如何處理的:只使用'fixed-delay'和'DirectChannels'。您只應將該外部服務的回覆轉移到ExecutorChannel中執行下一個邏輯並釋放當前的線程以便下一個輪詢任務通過Hibernate檢索新的實體列表。你還想聽聽什麼? –