Eclipse - 找工作
Eclipse - find a job
我想找一份特别的工作,停下来,然后再 运行。该作业是在 Eclipse 启动后立即安排的,并且已重新安排。以下:https://eclipse.org/articles/Article-Concurrency/jobs-api.html 给定文章的示例 3,我已经覆盖了 belongsTo 方法,但是当我使用 jobManager 查找工作时,它找不到它。
工作 class:
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。
要使此代码正常工作,PollFeedJob 中的 belongsTo() 方法应如下所示:
@Override
public boolean belongsTo(Object job) {
return Objects.equals(Constants.POLL_FEED_JOB_FAMILY, job);
}
因为 JobManager 查找 Constants.POLL_FEED_JOB_FAMILY belongsTo() 方法应该 return 基于该值。
我想找一份特别的工作,停下来,然后再 运行。该作业是在 Eclipse 启动后立即安排的,并且已重新安排。以下:https://eclipse.org/articles/Article-Concurrency/jobs-api.html 给定文章的示例 3,我已经覆盖了 belongsTo 方法,但是当我使用 jobManager 查找工作时,它找不到它。 工作 class:
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。
要使此代码正常工作,PollFeedJob 中的 belongsTo() 方法应如下所示:
@Override
public boolean belongsTo(Object job) {
return Objects.equals(Constants.POLL_FEED_JOB_FAMILY, job);
}
因为 JobManager 查找 Constants.POLL_FEED_JOB_FAMILY belongsTo() 方法应该 return 基于该值。