-
Notifications
You must be signed in to change notification settings - Fork 28
New feature: message with priority #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
StonesGitHub
wants to merge
8
commits into
crabhi:master
Choose a base branch
from
StonesGitHub:newfeature_addpriority
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b88f253
fix the bug that message is not sent to the right queue
909357a
Merge branch 'master' into newfeature_addpriority
StonesGitHub 1197844
fix the bug that message is not sent to the right queue
6f8db74
fix the bug that message is not sent to the right queue
1945ad6
fix the bug that message is not sent to the right queue
700e140
Exposed task id
729ed17
expose broker and backend connection to rabbitmq
3e8542a
change pom setting
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -55,15 +55,23 @@ public class Celery { | |
| private Celery(final String brokerUri, | ||
| @Nullable final String queue, | ||
| @Nullable final String backendUri, | ||
| @Nullable final ExecutorService executor) { | ||
| @Nullable final ExecutorService executor, | ||
| @Nullable final boolean isPriQueue, | ||
| @Nullable final int maxPriority) { | ||
| this.queue = queue == null ? "celery" : queue; | ||
|
|
||
| ExecutorService executorService = executor != null ? executor : Executors.newCachedThreadPool(); | ||
|
|
||
| broker = Suppliers.memoize(() -> { | ||
| Broker b = CeleryBrokers.createBroker(brokerUri, executorService); | ||
| try { | ||
| b.declareQueue(Celery.this.queue); | ||
| if(isPriQueue && maxPriority != 0){ | ||
| b.declarePriQueue(Celery.this.queue, maxPriority); | ||
| } | ||
| else { | ||
| b.declareQueue(Celery.this.queue); | ||
| } | ||
|
|
||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
|
|
@@ -110,6 +118,22 @@ public AsyncResult<?> submit(Class<?> taskClass, String method, Object[] args) t | |
| return submit(taskClass.getName() + "#" + method, args); | ||
| } | ||
|
|
||
| /** | ||
| * Submit a Java task for processing with priority. You'll probably not need to call this method. rather use @{@link CeleryTask} | ||
| * annotation. | ||
| * | ||
| * @param taskClass task implementing class | ||
| * @param method method in {@code taskClass} that does the work | ||
| * @param priority the priority of the task | ||
| * @param args positional arguments for the method (need to be JSON serializable) | ||
| * @return asynchronous result | ||
| * | ||
| * @throws IOException if the message couldn't be sent | ||
| */ | ||
| public AsyncResult<?> submitWithPri(Class<?> taskClass, String method, int priority, Object[] args) throws IOException { | ||
| return submitWithPri(taskClass.getName() + "#" + method, priority, args); | ||
| } | ||
|
|
||
| /** | ||
| * Submit a task by name. A low level method for submitting arbitrary tasks that don't have their proxies | ||
| * generated by @{@link CeleryTask} annotation. | ||
|
|
@@ -166,6 +190,61 @@ public AsyncResult<?> submit(String name, Object[] args) throws IOException { | |
| return new AsyncResultImpl<>(result); | ||
| } | ||
|
|
||
| /** | ||
| * Submit a task by name with priority. | ||
| * | ||
| * @param name task name as understood by the worker | ||
| * @param priority the priority of the message | ||
| * @param args positional arguments for the method (need to be JSON serializable) | ||
| * @return asynchronous result | ||
| * @throws IOException | ||
| */ | ||
| public AsyncResult<?> submitWithPri(String name, int priority, Object[] args) throws IOException { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer if this and other methods kept the same name as the existing ones, just overloaded by parameters. |
||
| // Get the provider early to increase the chance to find out there is a connection problem before actually | ||
| // sending the message. | ||
| // | ||
| // This will help for example in the case when the connection can't be established at all. The connection may | ||
| // still drop after sending the message but there isn't much we can do about it. | ||
| Optional<Backend.ResultsProvider> rp = resultsProvider.get(); | ||
| String taskId = UUID.randomUUID().toString(); | ||
|
|
||
| ArrayNode payload = jsonMapper.createArrayNode(); | ||
| ArrayNode argsArr = payload.addArray(); | ||
| for (Object arg : args) { | ||
| argsArr.addPOJO(arg); | ||
| } | ||
| payload.addObject(); | ||
| payload.addObject() | ||
| .putNull("callbacks") | ||
| .putNull("chain") | ||
| .putNull("chord") | ||
| .putNull("errbacks"); | ||
|
|
||
| Message message = broker.get().newMessageWithPriority(priority); | ||
| message.setBody(jsonMapper.writeValueAsBytes(payload)); | ||
| message.setContentEncoding("utf-8"); | ||
| message.setContentType("application/json"); | ||
|
|
||
| Message.Headers headers = message.getHeaders(); | ||
| headers.setId(taskId); | ||
| headers.setTaskName(name); | ||
| headers.setArgsRepr("(" + Joiner.on(", ").join(args) + ")"); | ||
| headers.setOrigin(clientName); | ||
| if (rp.isPresent()) { | ||
| headers.setReplyTo(clientId); | ||
| } | ||
|
|
||
| message.send(queue); | ||
|
|
||
| Future<Object> result; | ||
| if (rp.isPresent()) { | ||
| result = rp.get().getResult(taskId); | ||
| } else { | ||
| result = CompletableFuture.completedFuture(null); | ||
| } | ||
| return new AsyncResultImpl<>(result); | ||
| } | ||
|
|
||
| public interface AsyncResult<T> { | ||
| boolean isDone(); | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could
maxPrioritybe anOptional<Integer>? That way you wouldn't needisPriQueueparameter.