diff --git a/celery-java/pom.xml b/celery-java/pom.xml
index 4b186f8..7d3c2a1 100644
--- a/celery-java/pom.xml
+++ b/celery-java/pom.xml
@@ -7,7 +7,9 @@
4.0.0
+ org.sedlakovi.celerycelery-java
+ 1.3-SNAPSHOTjarCelery-Java
@@ -137,4 +139,22 @@
+
+ 1.8
+ 1.8
+
+
+
+
+
+ spring-releases
+ https://repo.spring.io/libs-release
+
+
+
+
+ spring-releases
+ https://repo.spring.io/libs-release
+
+
diff --git a/celery-java/src/main/java/com/geneea/celery/Celery.java b/celery-java/src/main/java/com/geneea/celery/Celery.java
index 7413a9a..acbcb92 100644
--- a/celery-java/src/main/java/com/geneea/celery/Celery.java
+++ b/celery-java/src/main/java/com/geneea/celery/Celery.java
@@ -3,8 +3,13 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.geneea.celery.backends.rabbit.RabbitResultConsumer;
+import com.geneea.celery.brokers.rabbit.RabbitBroker;
import com.google.common.base.Joiner;
import com.google.common.base.Suppliers;
+import com.rabbitmq.client.AMQP;
+import com.rabbitmq.client.Channel;
+import com.rabbitmq.client.Connection;
import lombok.Builder;
import lombok.extern.java.Log;
import com.geneea.celery.backends.CeleryBackends;
@@ -36,12 +41,12 @@ public class Celery {
private final ObjectMapper jsonMapper = new ObjectMapper();
private final String queue;
- // Memoized suppliers help us to deal with a connection that can't be established yet. It may fail several times
+ // Memorized suppliers help us to deal with a connection that can't be established yet. It may fail several times
// with an exception but when it succeeds, it then always returns the same instance.
//
// This is tailored for the RabbitMQ connections - they fail to be created if the host can't be reached but they
// can heal automatically. If other brokers/backends don't work this way, we might need to rework it.
- private final Supplier> resultsProvider;
+ public final Supplier> resultsProvider;
private final Supplier broker;
/**
@@ -49,13 +54,15 @@ public class Celery {
*
* @param brokerUri connection to broker that will dispatch messages
* @param backendUri connection to backend providing responses
+ * @param maxPriority the max priority of the queue if any, otherwise set to zero
* @param queue routing tag (specifies into which Rabbit queue the messages will go)
*/
@Builder
private Celery(final String brokerUri,
@Nullable final String queue,
@Nullable final String backendUri,
- @Nullable final ExecutorService executor) {
+ @Nullable final ExecutorService executor,
+ Optional maxPriority) {
this.queue = queue == null ? "celery" : queue;
ExecutorService executorService = executor != null ? executor : Executors.newCachedThreadPool();
@@ -63,7 +70,13 @@ private Celery(final String brokerUri,
broker = Suppliers.memoize(() -> {
Broker b = CeleryBrokers.createBroker(brokerUri, executorService);
try {
- b.declareQueue(Celery.this.queue);
+ if( maxPriority.isPresent()){
+ b.declareQueue(Celery.this.queue, maxPriority.get());
+ }
+ else {
+ b.declareQueue(Celery.this.queue);
+ }
+
} catch (IOException e) {
throw new RuntimeException(e);
}
@@ -95,6 +108,30 @@ private String getLocalHostName() {
}
}
+
+ public Connection getBrokerConnection(){
+ try{
+ RabbitBroker b = (RabbitBroker)broker.get();
+ Connection con = b.getChannel().getConnection();
+ return con;
+ }catch (Exception ex){
+ System.out.println(String.format("Can not get celery broker connection with ex:%s", ex.toString()));
+ return null;
+ }
+ }
+
+ public Connection getBackendConnection(){
+ try{
+ RabbitResultConsumer df = (RabbitResultConsumer)resultsProvider.get().get() ;
+
+ Connection conn = df.getChannel().getConnection();
+ return conn;
+ }catch (Exception ex){
+ System.out.println(String.format("Can not get celery backend connection with ex:%s", ex.toString()));
+ return null;
+ }
+ }
+
/**
* Submit a Java task for processing. You'll probably not need to call this method. rather use @{@link CeleryTask}
* annotation.
@@ -110,6 +147,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> submit(Class> taskClass, String method, int priority, Object[] args) throws IOException {
+ return submit(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.
@@ -142,6 +195,62 @@ public AsyncResult> submit(String name, Object[] args) throws IOException {
.putNull("errbacks");
Message message = broker.get().newMessage();
+
+ 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