Feed 配接器
Spring Integration 透過 feed 配接器提供對聯合內容 (syndication) 的支援。 實作基於 ROME Framework。
您需要在專案中加入此依賴項
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-feed</artifactId>
<version>6.3.5</version>
</dependency>
compile "org.springframework.integration:spring-integration-feed:6.3.5"
網路聯合內容是一種發布材料的方式,例如新聞報導、新聞稿、部落格文章和其他通常在網站上提供的項目,但也可以以 feed 格式(例如 RSS 或 ATOM)提供。
Spring Integration 透過其 'feed' 配接器提供網路聯合內容的支援,並為其提供方便的基於命名空間的設定。 若要設定 'feed' 命名空間,請在 XML 配置文件的標頭中包含下列元素
xmlns:int-feed="http://www.springframework.org/schema/integration/feed"
xsi:schemaLocation="http://www.springframework.org/schema/integration/feed
https://www.springframework.org/schema/integration/feed/spring-integration-feed.xsd"
Feed 輸入通道配接器
您真正需要提供的唯一配接器,以支援檢索 feeds 是輸入通道配接器。 它可讓您訂閱特定的 URL。 以下範例顯示可能的設定
-
Java DSL
-
Java
-
XML
@Configuration
@EnableIntegration
public class ContextConfiguration {
@Value("org/springframework/integration/feed/sample.rss")
private Resource feedResource;
@Bean
public IntegrationFlow feedFlow() {
return IntegrationFlow
.from(Feed.inboundAdapter(this.feedResource, "feedTest")
.preserveWireFeed(true),
e -> e.poller(p -> p.fixedDelay(100)))
.channel(c -> c.queue("entries"))
.get();
}
}
@Bean
@InboundChannelAdapter(inputChannel = "fromFeed")
public FeedEntryMessageSource feedEntrySource() {
return new FeedEntryMessageSource("https://feeds.bbci.co.uk/news/rss.xml", "metadataKey");
}
<int-feed:inbound-channel-adapter id="feedAdapter"
channel="feedChannel"
url="https://feeds.bbci.co.uk/news/rss.xml">
<int:poller fixed-rate="10000" max-messages-per-poll="100" />
</int-feed:inbound-channel-adapter>
在先前的設定中,我們正在訂閱由 `url` 屬性識別的 URL。
當檢索到新聞項目時,它們會轉換為訊息並發送到由 `channel` 屬性識別的通道。 每個訊息的酬載都是 `com.rometools.rome.feed.synd.SyndEntry` 實例。 每個實例都封裝了有關新聞項目的各種數據(內容、日期、作者和其他詳細資訊)。
輸入 feed 通道配接器是輪詢消費者。 這表示您必須提供 poller 設定。 但是,關於 feed,您必須了解的一件重要事情是,它的內部運作方式與大多數其他輪詢消費者略有不同。 當輸入 feed 配接器啟動時,它會執行第一次輪詢並接收 `com.rometools.rome.feed.synd.SyndFeed` 實例。 該物件包含多個 `SyndEntry` 物件。 每個項目都儲存在本機項目佇列中,並根據 `max-messages-per-poll` 屬性中的值釋放,以便每個訊息包含單個項目。 如果在從項目佇列檢索項目的過程中,佇列已變空,則配接器會嘗試更新 feed,從而使用更多項目(`SyndEntry` 實例)填充佇列(如果有任何可用)。 否則,下一次嘗試輪詢 feed 的時間由 poller 的觸發器決定(在先前的設定中為每十秒)。
重複項目
輪詢 feed 可能會導致已處理過的項目(「我已經讀過這則新聞項目了,為什麼你還要再顯示給我看?」)。 Spring Integration 提供了一種方便的機制,可以消除對重複項目感到擔憂的需求。 每個 feed 項目都有一個「發布日期」欄位。 每次產生並發送新的 `Message` 時,Spring Integration 都會在 `MetadataStore` 策略的實例中儲存最新發布日期的值(請參閱 Metadata Store)。 `metadataKey` 用於持久化最新發布日期。
其他選項
從 5.0 版開始,已移除已棄用的 `com.rometools.fetcher.FeedFetcher` 選項,並為 `org.springframework.core.io.Resource` 提供了多載的 `FeedEntryMessageSource` 建構子。 當 feed 來源不是 HTTP 端點而是任何其他資源(例如本機或遠端 FTP 上的資源)時,這非常有用。 在 `FeedEntryMessageSource` 邏輯中,此類資源(或提供的 `URL`)由 `SyndFeedInput` 解析為 `SyndFeed` 物件,以進行前面提到的處理。 您也可以將自訂的 `SyndFeedInput`(例如,帶有 `allowDoctypes` 選項)實例注入到 `FeedEntryMessageSource` 中。
如果 feed 的連線需要一些自訂,例如連線和讀取逾時,則必須使用 `org.springframework.core.io.UrlResource` 擴充及其 `customizeConnection(HttpURLConnection)` 覆寫,而不是將純 `URL` 注入到 `FeedEntryMessageSource` 中。 例如
|