Search in sources :

Example 1 with ChangeMessage

use of org.sagebionetworks.repo.model.message.ChangeMessage in project Synapse-Repository-Services by Sage-Bionetworks.

the class MessageUtils method createMessage.

/**
 * Create an Amazon message from a ChangeMessage.  This is used for testing.
 * @param message
 * @return
 */
public static Message createMessage(ChangeMessage message, String messageId, String receiptHandle) {
    if (message == null)
        throw new IllegalArgumentException("Message cannot be null");
    try {
        Message result = new Message().withMessageId(messageId).withReceiptHandle(receiptHandle);
        result.setBody(EntityFactory.createJSONStringForEntity(message));
        return result;
    } catch (JSONObjectAdapterException e) {
        throw new RuntimeException(e);
    }
}
Also used : ChangeMessage(org.sagebionetworks.repo.model.message.ChangeMessage) Message(com.amazonaws.services.sqs.model.Message) JSONObjectAdapterException(org.sagebionetworks.schema.adapter.JSONObjectAdapterException)

Example 2 with ChangeMessage

use of org.sagebionetworks.repo.model.message.ChangeMessage in project Synapse-Repository-Services by Sage-Bionetworks.

the class MessageUtils method extractMessageBody.

/**
 * Extract a ChangeMessage from an Amazon Message
 * @param message
 * @return
 */
public static ChangeMessage extractMessageBody(Message message) {
    if (message == null)
        throw new IllegalArgumentException("Message cannot be null");
    try {
        JSONObject object = new JSONObject(message.getBody());
        if (object.has("objectId")) {
            // This is a message pushed directly to a queue
            JSONObjectAdapterImpl adapter = new JSONObjectAdapterImpl(object);
            return new ChangeMessage(adapter);
        }
        if (object.has("TopicArn") && object.has("Message")) {
            // This is a message that was pushed to a topic then forwarded to a queue.
            JSONObject innerObject = new JSONObject(object.getString("Message"));
            JSONObjectAdapterImpl adapter = new JSONObjectAdapterImpl(innerObject);
            return new ChangeMessage(adapter);
        } else {
            throw new IllegalArgumentException("Unknown message type: " + message.getBody());
        }
    } catch (JSONException e) {
        throw new RuntimeException(e);
    } catch (JSONObjectAdapterException e) {
        throw new RuntimeException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) ChangeMessage(org.sagebionetworks.repo.model.message.ChangeMessage) JSONException(org.json.JSONException) JSONObjectAdapterException(org.sagebionetworks.schema.adapter.JSONObjectAdapterException) JSONObjectAdapterImpl(org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl)

Example 3 with ChangeMessage

use of org.sagebionetworks.repo.model.message.ChangeMessage in project Synapse-Repository-Services by Sage-Bionetworks.

the class TagMessengerImpl method generateEtagAndSendMessage.

@Override
public void generateEtagAndSendMessage(ObservableEntity observable, ChangeType changeType) {
    // Send a message that an entity was created
    String newEtag = eTagGenerator.generateETag(observable);
    observable.seteTag(newEtag);
    // Create the message
    ChangeMessage message = new ChangeMessage();
    message.setChangeType(changeType);
    message.setObjectType(observable.getObjectType());
    message.setObjectId(observable.getIdString());
    message.setParentId(observable.getParentIdString());
    message.setObjectEtag(observable.geteTag());
    transactionalMessanger.sendMessageAfterCommit(message);
}
Also used : ChangeMessage(org.sagebionetworks.repo.model.message.ChangeMessage)

Example 4 with ChangeMessage

use of org.sagebionetworks.repo.model.message.ChangeMessage in project Synapse-Repository-Services by Sage-Bionetworks.

the class SearchQueueWorker method call.

@Override
public List<Message> call() throws Exception {
    // Process each message
    for (Message message : messagesToProcess) {
        // Extract the ChangeMessage
        ChangeMessage change = MessageUtils.extractMessageBody(message);
        // We only care about entity messages as this time
        if (ObjectType.ENTITY == change.getObjectType()) {
            // Is this a create or update
            if (ChangeType.CREATE == change.getChangeType() || ChangeType.UPDATE == change.getChangeType()) {
                addCreateOrUpdateEntity(change);
            } else if (ChangeType.DELETE == change.getChangeType()) {
                addDeleteEntity(change);
            } else {
                throw new IllegalArgumentException("Unknown change type: " + change.getChangeType() + " for messageID =" + message.getMessageId());
            }
        }
    }
    processCreateUpdateBatch();
    processDeleteBatch();
    return messagesToProcess;
}
Also used : ChangeMessage(org.sagebionetworks.repo.model.message.ChangeMessage) Message(com.amazonaws.services.sqs.model.Message) ChangeMessage(org.sagebionetworks.repo.model.message.ChangeMessage)

Example 5 with ChangeMessage

use of org.sagebionetworks.repo.model.message.ChangeMessage in project Synapse-Repository-Services by Sage-Bionetworks.

the class SearchQueueWorker method processCreateUpdateBatch.

/**
 * Send the documents to search.
 * @throws DatastoreException
 * @throws NotFoundException
 * @throws ClientProtocolException
 * @throws IOException
 * @throws HttpClientHelperException
 */
private void processCreateUpdateBatch() throws DatastoreException, NotFoundException, ClientProtocolException, IOException, HttpClientHelperException {
    if (createOrUpdateMessages != null) {
        log.debug("Processing " + createOrUpdateMessages.size() + " create/update messages");
        // Prepare a batch of documents
        List<Document> batch = new LinkedList<Document>();
        for (ChangeMessage message : createOrUpdateMessages) {
            // We want to ignore this message if a document with this ID and Etag already exists in the search index.
            if (!searchDao.doesDocumentExist(message.getObjectId(), message.getObjectEtag())) {
                // We want to ignore this message if a document with this ID and Etag are not in the repository as it is an old message.
                if (documentProvider.doesDocumentExist(message.getObjectId(), message.getObjectEtag())) {
                    // Create a document for this
                    Document newDoc = documentProvider.formulateSearchDocument(message.getObjectId());
                    batch.add(newDoc);
                }
            }
        }
        if (!batch.isEmpty()) {
            searchDao.createOrUpdateSearchDocument(batch);
        }
    }
}
Also used : ChangeMessage(org.sagebionetworks.repo.model.message.ChangeMessage) Document(org.sagebionetworks.repo.model.search.Document) LinkedList(java.util.LinkedList)

Aggregations

ChangeMessage (org.sagebionetworks.repo.model.message.ChangeMessage)31 Test (org.junit.Test)17 Message (com.amazonaws.services.sqs.model.Message)6 ArrayList (java.util.ArrayList)4 PublishRequest (com.amazonaws.services.sns.model.PublishRequest)3 Node (org.sagebionetworks.repo.model.Node)3 JSONObjectAdapterException (org.sagebionetworks.schema.adapter.JSONObjectAdapterException)3 LinkedList (java.util.LinkedList)2 JSONObject (org.json.JSONObject)2 HashSet (java.util.HashSet)1 JSONException (org.json.JSONException)1 ConflictingUpdateException (org.sagebionetworks.repo.model.ConflictingUpdateException)1 DatastoreException (org.sagebionetworks.repo.model.DatastoreException)1 InvalidModelException (org.sagebionetworks.repo.model.InvalidModelException)1 TransactionalMessengerObserver (org.sagebionetworks.repo.model.message.TransactionalMessengerObserver)1 Document (org.sagebionetworks.repo.model.search.Document)1 NotFoundException (org.sagebionetworks.repo.web.NotFoundException)1 JSONObjectAdapterImpl (org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl)1 Transactional (org.springframework.transaction.annotation.Transactional)1