使用Spring Boot和Spring Integration获取RSS的示例

本示例展示了如何使用Spring Boot和Spring Integration来获取RSS源,而无需使用XML,只需使用Java配置就可以完成。

需要

请提前安装以下软件。

    • JDK 6 or later

 

    Maven 3.0 or later

pom.xml可以进行重写。

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.0.M2</version>
        <relativePath/>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-integration</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-feed</artifactId>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone/</url>
        </repository>
    </repositories>

Application.java 应用程序.java

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
    private static final Logger LOG = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) throws Exception {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
        System.in.read();
        Runtime.getRuntime().exit(SpringApplication.exit(ctx));
    }

    @Autowired
    private Environment env;

    @Bean
    @InboundChannelAdapter(value = "feedChannel",
            poller = @Poller(maxMessagesPerPoll = "100", fixedRate = "10000"))
    public MessageSource<SyndEntry> feedAdapter() throws MalformedURLException {
        return new FeedEntryMessageSource(new URL(env.getProperty("url")), "feedAdapter");
    }

    @MessageEndpoint
    public static class Endpoint {
        @ServiceActivator(inputChannel = "feedChannel")
        public void log(Message<SyndEntry> message) {
            SyndEntry payload = message.getPayload();
            LOG.info(payload.getPublishedDate() + " - " + payload.getTitle());
        }
    }

    @Bean
    public MessageChannel feedChannel() {
        return new QueueChannel(500);
    }

    // <int:poller id="poller" default="true" fixed-rate="10"/>
    @Bean(name = PollerMetadata.DEFAULT_POLLER)
    public PollerMetadata poller() {
        PeriodicTrigger trigger = new PeriodicTrigger(10);
        trigger.setFixedRate(true);
        PollerMetadata pollerMetadata = new PollerMetadata();
        pollerMetadata.setTrigger(trigger);
        return pollerMetadata;
    }
}

执行方式和执行示例

当你在url选项中指定feed-url并执行时,会以10秒的间隔访问feed-url,并获取每次最多100个RSS条目。
获取的RSS条目会输出到日志中。

要从”http://search.goo.ne.jp/rss/newkw.rdf”获取RSS条目,您可以按照以下方式执行。

mvn package
java -jar target/spring-boot-integration-rss-sample-1.0.jar --url=http://search.goo.ne.jp/rss/newkw.rdf

完整的源代码

请访问 https://github.com/sunny4381/spring-boot-integration-rss-sample,并执行。

git clone https://github.com/sunny4381/spring-boot-integration-rss-sample.git
广告
将在 10 秒后关闭
bannerAds