Search in sources :

Example 11 with Message

use of com.netflix.conductor.core.events.queue.Message in project conductor by Netflix.

the class AMQPObservableQueue method ack.

@Override
public List<String> ack(List<Message> messages) {
    final List<String> processedDeliveryTags = new ArrayList<>();
    for (final Message message : messages) {
        try {
            logger.info("ACK message with delivery tag {}", message.getReceipt());
            getOrCreateChannel().basicAck(Long.valueOf(message.getReceipt()), false);
            // Message ACKed
            processedDeliveryTags.add(message.getReceipt());
        } catch (final IOException e) {
            logger.error("Cannot ACK message with delivery tag {}", message.getReceipt(), e);
        }
    }
    return processedDeliveryTags;
}
Also used : Message(com.netflix.conductor.core.events.queue.Message) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 12 with Message

use of com.netflix.conductor.core.events.queue.Message in project conductor by Netflix.

the class NATSAbstractQueue method observe.

@Override
public Observable<Message> observe() {
    logger.info("Observe invoked for queueURI " + queueURI);
    observable = true;
    mu.lock();
    try {
        subscribe();
    } finally {
        mu.unlock();
    }
    Observable.OnSubscribe<Message> onSubscribe = subscriber -> {
        Observable<Long> interval = Observable.interval(100, TimeUnit.MILLISECONDS, scheduler);
        interval.flatMap((Long x) -> {
            List<Message> available = new LinkedList<>();
            messages.drainTo(available);
            if (!available.isEmpty()) {
                AtomicInteger count = new AtomicInteger(0);
                StringBuilder buffer = new StringBuilder();
                available.forEach(msg -> {
                    buffer.append(msg.getId()).append("=").append(msg.getPayload());
                    count.incrementAndGet();
                    if (count.get() < available.size()) {
                        buffer.append(",");
                    }
                });
                logger.info(String.format("Batch from %s to conductor is %s", subject, buffer.toString()));
            }
            return Observable.from(available);
        }).subscribe(subscriber::onNext, subscriber::onError);
    };
    return Observable.create(onSubscribe);
}
Also used : Message(com.netflix.conductor.core.events.queue.Message) Logger(org.slf4j.Logger) ReentrantLock(java.util.concurrent.locks.ReentrantLock) LoggerFactory(org.slf4j.LoggerFactory) Scheduler(rx.Scheduler) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Executors(java.util.concurrent.Executors) Observable(rx.Observable) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Lock(java.util.concurrent.locks.Lock) NUID(io.nats.client.NUID) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) LinkedList(java.util.LinkedList) Collections(java.util.Collections) ObservableQueue(com.netflix.conductor.core.events.queue.ObservableQueue) Message(com.netflix.conductor.core.events.queue.Message) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Observable(rx.Observable) LinkedList(java.util.LinkedList)

Example 13 with Message

use of com.netflix.conductor.core.events.queue.Message in project conductor by Netflix.

the class NATSAbstractQueue method onMessage.

void onMessage(String subject, byte[] data) {
    String payload = new String(data);
    logger.info(String.format("Received message for %s: %s", subject, payload));
    Message dstMsg = new Message();
    dstMsg.setId(NUID.nextGlobal());
    dstMsg.setPayload(payload);
    messages.add(dstMsg);
}
Also used : Message(com.netflix.conductor.core.events.queue.Message)

Example 14 with Message

use of com.netflix.conductor.core.events.queue.Message in project conductor by Netflix.

the class SQSObservableQueue method receiveMessages.

@VisibleForTesting
List<Message> receiveMessages() {
    try {
        ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest().withQueueUrl(queueURL).withVisibilityTimeout(visibilityTimeoutInSeconds).withMaxNumberOfMessages(batchSize);
        ReceiveMessageResult result = client.receiveMessage(receiveMessageRequest);
        List<Message> messages = result.getMessages().stream().map(msg -> new Message(msg.getMessageId(), msg.getBody(), msg.getReceiptHandle())).collect(Collectors.toList());
        Monitors.recordEventQueueMessagesProcessed(QUEUE_TYPE, this.queueName, messages.size());
        return messages;
    } catch (Exception e) {
        logger.error("Exception while getting messages from SQS", e);
        Monitors.recordObservableQMessageReceivedErrors(QUEUE_TYPE);
    }
    return new ArrayList<>();
}
Also used : Policy(com.amazonaws.auth.policy.Policy) ChangeMessageVisibilityRequest(com.amazonaws.services.sqs.model.ChangeMessageVisibilityRequest) Action(com.amazonaws.auth.policy.Action) Principal(com.amazonaws.auth.policy.Principal) SetQueueAttributesResult(com.amazonaws.services.sqs.model.SetQueueAttributesResult) DeleteMessageBatchRequest(com.amazonaws.services.sqs.model.DeleteMessageBatchRequest) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) BatchResultErrorEntry(com.amazonaws.services.sqs.model.BatchResultErrorEntry) SendMessageBatchRequest(com.amazonaws.services.sqs.model.SendMessageBatchRequest) ReceiveMessageResult(com.amazonaws.services.sqs.model.ReceiveMessageResult) Observable(rx.Observable) ArrayList(java.util.ArrayList) DeleteMessageBatchResult(com.amazonaws.services.sqs.model.DeleteMessageBatchResult) Map(java.util.Map) DeleteMessageBatchRequestEntry(com.amazonaws.services.sqs.model.DeleteMessageBatchRequestEntry) Statement(com.amazonaws.auth.policy.Statement) SQSActions(com.amazonaws.auth.policy.actions.SQSActions) CreateQueueResult(com.amazonaws.services.sqs.model.CreateQueueResult) ReceiveMessageRequest(com.amazonaws.services.sqs.model.ReceiveMessageRequest) LinkedList(java.util.LinkedList) Message(com.netflix.conductor.core.events.queue.Message) Logger(org.slf4j.Logger) AmazonSQSClient(com.amazonaws.services.sqs.AmazonSQSClient) SendMessageBatchResult(com.amazonaws.services.sqs.model.SendMessageBatchResult) ListQueuesResult(com.amazonaws.services.sqs.model.ListQueuesResult) GetQueueAttributesResult(com.amazonaws.services.sqs.model.GetQueueAttributesResult) Scheduler(rx.Scheduler) Collectors(java.util.stream.Collectors) Effect(com.amazonaws.auth.policy.Statement.Effect) SendMessageBatchRequestEntry(com.amazonaws.services.sqs.model.SendMessageBatchRequestEntry) Monitors(com.netflix.conductor.metrics.Monitors) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Resource(com.amazonaws.auth.policy.Resource) CreateQueueRequest(com.amazonaws.services.sqs.model.CreateQueueRequest) ListQueuesRequest(com.amazonaws.services.sqs.model.ListQueuesRequest) OnSubscribe(rx.Observable.OnSubscribe) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ObservableQueue(com.netflix.conductor.core.events.queue.ObservableQueue) Collections(java.util.Collections) ReceiveMessageRequest(com.amazonaws.services.sqs.model.ReceiveMessageRequest) Message(com.netflix.conductor.core.events.queue.Message) ArrayList(java.util.ArrayList) ReceiveMessageResult(com.amazonaws.services.sqs.model.ReceiveMessageResult) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 15 with Message

use of com.netflix.conductor.core.events.queue.Message in project conductor by Netflix.

the class TestQueueManager method setup.

@BeforeClass
public static void setup() throws Exception {
    queue = mock(SQSObservableQueue.class);
    when(queue.getOrCreateQueue()).thenReturn("junit_queue_url");
    Answer<?> answer = new Answer<List<Message>>() {

        @Override
        public List<Message> answer(InvocationOnMock invocation) throws Throwable {
            List<Message> copy = new LinkedList<>();
            copy.addAll(messages);
            messages.clear();
            return copy;
        }
    };
    when(queue.receiveMessages()).thenAnswer(answer);
    when(queue.getOnSubscribe()).thenCallRealMethod();
    when(queue.observe()).thenCallRealMethod();
    when(queue.getName()).thenReturn(Status.COMPLETED.name());
    Task task0 = new Task();
    task0.setStatus(Status.IN_PROGRESS);
    task0.setTaskId("t0");
    task0.setReferenceTaskName("t0");
    task0.setTaskType(Wait.NAME);
    Workflow workflow0 = new Workflow();
    workflow0.setWorkflowId("v_0");
    workflow0.getTasks().add(task0);
    Task task2 = new Task();
    task2.setStatus(Status.IN_PROGRESS);
    task2.setTaskId("t2");
    task2.setTaskType(Wait.NAME);
    Workflow workflow2 = new Workflow();
    workflow2.setWorkflowId("v_2");
    workflow2.getTasks().add(task2);
    doAnswer(new Answer<Void>() {

        @SuppressWarnings("unchecked")
        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            List<Message> msgs = invocation.getArgument(0, List.class);
            System.out.println("got messages to publish: " + msgs);
            messages.addAll(msgs);
            return null;
        }
    }).when(queue).publish(any());
    es = mock(ExecutionService.class);
    assertNotNull(es);
    doReturn(workflow0).when(es).getExecutionStatus(eq("v_0"), anyBoolean());
    doReturn(workflow2).when(es).getExecutionStatus(eq("v_2"), anyBoolean());
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            System.out.println("Updating task: " + invocation.getArgument(0, Task.class));
            updatedTasks.add(invocation.getArgument(0, Task.class));
            return null;
        }
    }).when(es).updateTask(any(Task.class));
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task) Message(com.netflix.conductor.core.events.queue.Message) Workflow(com.netflix.conductor.common.run.Workflow) ExecutionService(com.netflix.conductor.service.ExecutionService) LinkedList(java.util.LinkedList) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LinkedList(java.util.LinkedList) List(java.util.List) BeforeClass(org.junit.BeforeClass)

Aggregations

Message (com.netflix.conductor.core.events.queue.Message)42 ArrayList (java.util.ArrayList)18 Test (org.junit.Test)16 LinkedList (java.util.LinkedList)12 List (java.util.List)11 ObservableQueue (com.netflix.conductor.core.events.queue.ObservableQueue)10 HashMap (java.util.HashMap)10 Collections (java.util.Collections)9 Map (java.util.Map)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 Connection (java.sql.Connection)7 Executors (java.util.concurrent.Executors)7 Task (com.netflix.conductor.common.metadata.tasks.Task)6 TimeUnit (java.util.concurrent.TimeUnit)6 Workflow (com.netflix.conductor.common.run.Workflow)5 QueueDAO (com.netflix.conductor.dao.QueueDAO)5 Collectors (java.util.stream.Collectors)5 SearchHit (org.elasticsearch.search.SearchHit)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5