1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.rssaggregator.importhandler;
35
36 import info.magnolia.module.rssaggregator.util.Assert;
37
38 import java.net.URL;
39
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import com.rometools.fetcher.FeedFetcher;
44 import com.rometools.rome.feed.synd.SyndFeed;
45
46
47
48
49
50 public class DefaultFeedChannelFetchTask implements Runnable {
51
52 private static final Logger log = LoggerFactory.getLogger(DefaultFeedChannelFetchTask.class);
53
54 private final FeedChannel channel;
55 private final String aggregateName;
56 private FeedFetcher feedFetcher;
57
58 public DefaultFeedChannelFetchTask(FeedFetcher feedFetcher, FeedChannel feedChannel, String aggregateName) {
59 Assert.notNull(feedFetcher, "'feedFetcher' must not be null");
60 Assert.notNull(feedChannel, "'feedChannel' must not be null");
61 Assert.notNull(aggregateName, "'aggregateName' must not be null");
62 this.feedFetcher = feedFetcher;
63 this.channel = feedChannel;
64 this.aggregateName = aggregateName;
65 }
66
67 @Override
68 public void run() {
69 final long threadId = Thread.currentThread().getId();
70 try {
71 log.debug("Fetching feed channel '{}' for aggregate '{}' (Thread {})", channel.getUrl(), aggregateName, threadId);
72 SyndFeed feed = feedFetcher.retrieveFeed(new URL(channel.getUrl()));
73 if (feed != null) {
74 channel.setFeed(feed);
75 log.debug("Fetched feed channel '{}' for aggregate '{}' (Thread {})", channel.getUrl(), aggregateName, threadId);
76 }
77 } catch (Exception e) {
78 log.error("Failed to fetch result for channel '{}' for aggregate '{}' (Thread {}): {}", channel.getUrl(), aggregateName, threadId, e);
79 }
80 }
81 }