-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use JobAttribute to specify different job requirements (#8)
* Separated Scheduler interface * Remove excess code in ProxyProvider * Refactor job/Scheduler to QueueScheduler * Updated UrlRouter to threadsafe * Fixed CrawlerTest * Minor refactoring * Minor refactoring * Modified AsyncFetcher.Builder and Crawler.Builder to fail fast * Added tests for Crawler.Builder and AsyncFetcher.Builder * Updates to job package * Updated Javadoc * Fixed warnings * Removed whitespace * Updated Job * Updated Job * Added tests * Create backward compatibility * Updated Javadoc * Only allow NotNull JobAttributes * Updated AsyncFetcher * Renamed addJobAttribute to setJobAttribute * Remove removeAndAdd() from QueueScheduler * Replace PendingJobs with AtomicInteger and rename
- Loading branch information
Showing
34 changed files
with
1,251 additions
and
542 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/ai/preferred/venom/job/AbstractPriorityQueueScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (c) 2019 Preferred.AI | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package ai.preferred.venom.job; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.util.Comparator; | ||
import java.util.concurrent.PriorityBlockingQueue; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* @author Ween Jiann Lee | ||
*/ | ||
public abstract class AbstractPriorityQueueScheduler extends AbstractQueueScheduler { | ||
|
||
/** | ||
* Constructs an instance of AbstractQueueScheduler. | ||
*/ | ||
protected AbstractPriorityQueueScheduler() { | ||
super(new PriorityBlockingQueue<>(11, | ||
Comparator.comparing(o -> (o.getJobAttribute(PriorityJobAttribute.class))))); | ||
} | ||
|
||
/** | ||
* Check the job for {@see PriorityJobAttribute}, if missing, | ||
* adds it to the job. | ||
* | ||
* @param job the job to check. | ||
* @return the input job. | ||
*/ | ||
private Job ensurePriorityJobAttribute(final Job job) { | ||
if (job.getJobAttribute(PriorityJobAttribute.class) == null) { | ||
job.setJobAttribute(new PriorityJobAttribute()); | ||
} | ||
return job; | ||
} | ||
|
||
@Override | ||
public final void put(final @Nonnull Job job) throws InterruptedException { | ||
getQueue().put(ensurePriorityJobAttribute(job)); | ||
} | ||
|
||
@Override | ||
public final boolean offer(final Job job, final long timeout, final @Nonnull TimeUnit unit) | ||
throws InterruptedException { | ||
return getQueue().offer(ensurePriorityJobAttribute(job), timeout, unit); | ||
} | ||
|
||
@Override | ||
public final boolean offer(final @Nonnull Job job) { | ||
return getQueue().offer(ensurePriorityJobAttribute(job)); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.