Eclipse - 找工作

时间:2015-06-09 13:45:35

标签: eclipse-plugin

我想找到特定的工作并停下来再跑一次。这项工作安排在Eclipse启动之后,并重新安排。以下:给定文章的https://eclipse.org/articles/Article-Concurrency/jobs-api.html示例3,我已经覆盖了belongsTo方法,但是当我使用jobManager查找作业时,它无法找到它。 工作班:

public class PollFeedJob extends Job implements IPollFeedJob {
private final String jobId;
private final NotificationEnvironment environment;
private final Map<FeedDescriptor, List<IFeedMessage>> groupedMessages = Maps.newHashMap();
private final Map<FeedDescriptor, Date> pollDates = Maps.newHashMap();

private Set<FeedDescriptor> feeds = Sets.newHashSet();

public PollFeedJob(String jobId, Collection<FeedDescriptor> feeds) {
    super(jobId);
    Preconditions.checkNotNull(jobId);
    Preconditions.checkNotNull(feeds);
    this.jobId = jobId;
    this.environment = new NotificationEnvironment();
    this.feeds.addAll(feeds);
    setSystem(true);
    setPriority(DECORATE);
    setRule(new MutexRule());
}

@Override
protected IStatus run(IProgressMonitor monitor) {
    try {
        for (FeedDescriptor feed : feeds) {
            List<IFeedMessage> messages;
            HttpURLConnection connection = (HttpURLConnection) feed.getUrl().openConnection();
            try {
                connection.connect();
                if (connection.getResponseCode() == HttpURLConnection.HTTP_OK && !monitor.isCanceled()) {
                    InputStream in = new BufferedInputStream(connection.getInputStream());
                    try {
                        messages = Lists.newArrayList(readMessages(in, monitor, feed.getId()));
                        groupedMessages.put(feed, messages);
                    } finally {
                        in.close();
                    }
                }
            } finally {
                connection.disconnect();
            }
            pollDates.put(feed, new Date());
        }
    } catch (Exception e) {
        System.out.println(e.getCause());
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}

@Override
public boolean belongsTo(Object job) {
    if (job == null) {
        return false;
    }
    return jobId.equals(job);
}

private List<? extends IFeedMessage> readMessages(InputStream in, IProgressMonitor monitor, String eventId)
        throws IOException {
    //not related code
}

@Override
public Map<FeedDescriptor, List<IFeedMessage>> getMessages() {
    return groupedMessages;
}

@Override
public Map<FeedDescriptor, Date> getPollDates() {
    return pollDates;
}

public String getJobId() {
    return jobId;
}

class MutexRule implements ISchedulingRule {

    @Override
    public boolean contains(ISchedulingRule rule) {
        return rule == this;
    }

    @Override
    public boolean isConflicting(ISchedulingRule rule) {
        return rule == this;
    }

}

@Override
public void setFeeds(Set<FeedDescriptor> feeds) {
    this.feeds = feeds;
}
}

作业按以下方法安排:

@Override
public void schedule(Collection<FeedDescriptor> feeds, final INewsService service) {
    final PollFeedJob job = new PollFeedJob(Constants.JOB_FAMILY, feeds);
    job.addJobChangeListener(new JobChangeAdapter() {
        @Override
        public void done(IJobChangeEvent event) {
            service.jobDone(job);
        }
    });
    job.schedule();
}

我试着通过调用:

找到它
IJobManager manager = Job.getJobManager();
System.out.println(manager.find(Constants.JOB_FAMILY).length);

但总是评估为0.

1 个答案:

答案 0 :(得分:0)

要使此代码生效,PollFeedJob中的belongsTo()方法应如下所示:

@Override
public boolean belongsTo(Object job) {
    return Objects.equals(Constants.POLL_FEED_JOB_FAMILY, job);
}

因为JobManager查找Constants.POLL_FEED_JOB_FAMILY,所以belongsTo()方法应该根据该值返回。