I'd like to find particular job and stop it and run again. The job is scheduled just after Eclipse start and it's rescheduled. Following: https://eclipse.org/articles/Article-Concurrency/jobs-api.html example 3 of given article, I've overriden belongsTo method, however when I use jobManager to find the job it can't find it. Job 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;
}
}
Job is scheduled in the following method:
@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();
}
and I try to find it, by calling:
IJobManager manager = Job.getJobManager();
System.out.println(manager.find(Constants.JOB_FAMILY).length);
However it always evaluates to 0.
For this code to work the belongsTo() method in PollFeedJob should look like this:
because JobManager looks for Constants.POLL_FEED_JOB_FAMILY the belongsTo() method should return based on that value.