-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ActorConfig based support to specify required message metadata …
…generators
- Loading branch information
1 parent
add4660
commit 5f4b972
Showing
21 changed files
with
738 additions
and
51 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
26 changes: 23 additions & 3 deletions
26
src/main/java/io/appform/dropwizard/actors/actor/MessageMetadata.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 |
---|---|---|
@@ -1,15 +1,35 @@ | ||
package io.appform.dropwizard.actors.actor; | ||
|
||
import com.rabbitmq.client.AMQP; | ||
import java.util.Date; | ||
import java.util.Map; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public final class MessageMetadata { | ||
|
||
private String contentType; | ||
private String contentEncoding; | ||
private Map<String,Object> headers; | ||
private Integer deliveryMode; | ||
private Integer priority; | ||
private String correlationId; | ||
private String replyTo; | ||
private String expiration; | ||
private String messageId; | ||
private Date msgSentTimestamp; | ||
private String type; | ||
private String userId; | ||
private String appId; | ||
private String clusterId; | ||
|
||
// custom fields | ||
private boolean redelivered; | ||
private long delayInMs; | ||
private AMQP.BasicProperties properties; | ||
|
||
private boolean expired; | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/io/appform/dropwizard/actors/actor/metadata/MessageDelayMetaGenerator.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,24 @@ | ||
package io.appform.dropwizard.actors.actor.metadata; | ||
|
||
import static io.appform.dropwizard.actors.common.Constants.MESSAGE_PUBLISHED_TEXT; | ||
|
||
import io.appform.dropwizard.actors.actor.MessageMetadata; | ||
import io.appform.dropwizard.actors.base.utils.MessageMetaUtils; | ||
import java.time.Instant; | ||
import java.util.Optional; | ||
|
||
public class MessageDelayMetaGenerator implements MessageMetadataGenerator { | ||
|
||
@Override | ||
public void generate(final MessageMetaContext messageMetaContext, MessageMetadata messageMetadata) { | ||
messageMetadata.setDelayInMs(calculateDelayInMs(messageMetaContext)); | ||
} | ||
|
||
private long calculateDelayInMs(MessageMetaContext messageMetaContext) { | ||
Optional<Long> publishedTimestamp = MessageMetaUtils.extractMessagePropertiesHeader( | ||
messageMetaContext.getHeaders(), | ||
MESSAGE_PUBLISHED_TEXT, Long.class); | ||
return publishedTimestamp.map(aLong -> Math.max(Instant.now().toEpochMilli() - aLong, 0)) | ||
.orElse(-1L); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/io/appform/dropwizard/actors/actor/metadata/MessageExpiredMetaGenerator.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,23 @@ | ||
package io.appform.dropwizard.actors.actor.metadata; | ||
|
||
import static io.appform.dropwizard.actors.common.Constants.MESSAGE_EXPIRY_TEXT; | ||
|
||
import io.appform.dropwizard.actors.actor.MessageMetadata; | ||
import io.appform.dropwizard.actors.base.utils.MessageMetaUtils; | ||
import java.time.Instant; | ||
import java.util.Optional; | ||
|
||
public class MessageExpiredMetaGenerator implements MessageMetadataGenerator { | ||
|
||
@Override | ||
public void generate(MessageMetaContext messageMetaContext, MessageMetadata messageMetadata) { | ||
messageMetadata.setExpired(isExpired(messageMetaContext)); | ||
} | ||
|
||
private boolean isExpired(MessageMetaContext messageMetaContext) { | ||
Optional<Long> msgExpiry = MessageMetaUtils.extractMessagePropertiesHeader(messageMetaContext.getHeaders(), | ||
MESSAGE_EXPIRY_TEXT, Long.class); | ||
return msgExpiry.filter(expiry -> Instant.now().toEpochMilli() >= expiry).isPresent(); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/io/appform/dropwizard/actors/actor/metadata/MessageHeadersMetaGenerator.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,12 @@ | ||
package io.appform.dropwizard.actors.actor.metadata; | ||
|
||
import io.appform.dropwizard.actors.actor.MessageMetadata; | ||
|
||
public class MessageHeadersMetaGenerator implements MessageMetadataGenerator { | ||
|
||
@Override | ||
public void generate(final MessageMetaContext messageMetaContext, MessageMetadata messageMetadata) { | ||
messageMetadata.setHeaders(messageMetaContext.getHeaders()); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/io/appform/dropwizard/actors/actor/metadata/MessageMetaContext.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,30 @@ | ||
package io.appform.dropwizard.actors.actor.metadata; | ||
|
||
import java.util.Date; | ||
import java.util.Map; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
/** | ||
* Ref : <a href="https://www.rabbitmq.com/amqp-0-9-1-reference.html#class.basic">AMQP properties</a> | ||
*/ | ||
@Data | ||
@Builder | ||
public class MessageMetaContext { | ||
|
||
private boolean redelivered; | ||
private String contentType; | ||
private String contentEncoding; | ||
private Map<String,Object> headers; | ||
private Integer deliveryMode; | ||
private Integer priority; | ||
private String correlationId; | ||
private String replyTo; | ||
private String expiration; | ||
private String messageId; | ||
private Date timestamp; | ||
private String type; | ||
private String userId; | ||
private String appId; | ||
private String clusterId; | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/io/appform/dropwizard/actors/actor/metadata/MessageMetaInfoGenerator.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,24 @@ | ||
package io.appform.dropwizard.actors.actor.metadata; | ||
|
||
import io.appform.dropwizard.actors.actor.MessageMetadata; | ||
|
||
public class MessageMetaInfoGenerator implements MessageMetadataGenerator { | ||
|
||
@Override | ||
public void generate(final MessageMetaContext messageMetaContext, MessageMetadata messageMetadata) { | ||
messageMetadata.setContentType(messageMetaContext.getContentType()); | ||
messageMetadata.setContentEncoding(messageMetaContext.getContentEncoding()); | ||
messageMetadata.setDeliveryMode(messageMetaContext.getDeliveryMode()); | ||
messageMetadata.setPriority(messageMetaContext.getPriority()); | ||
messageMetadata.setCorrelationId(messageMetaContext.getCorrelationId()); | ||
messageMetadata.setReplyTo(messageMetaContext.getReplyTo()); | ||
messageMetadata.setExpiration(messageMetaContext.getExpiration()); | ||
messageMetadata.setMessageId(messageMetaContext.getMessageId()); | ||
messageMetadata.setMsgSentTimestamp(messageMetaContext.getTimestamp()); | ||
messageMetadata.setType(messageMetaContext.getType()); | ||
messageMetadata.setUserId(messageMetaContext.getUserId()); | ||
messageMetadata.setAppId(messageMetaContext.getAppId()); | ||
messageMetadata.setClusterId(messageMetaContext.getClusterId()); | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/io/appform/dropwizard/actors/actor/metadata/MessageMetadataGenerator.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,9 @@ | ||
package io.appform.dropwizard.actors.actor.metadata; | ||
|
||
import io.appform.dropwizard.actors.actor.MessageMetadata; | ||
|
||
public interface MessageMetadataGenerator { | ||
|
||
void generate(final MessageMetaContext messageMetaContext, MessageMetadata messageMetadata); | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/io/appform/dropwizard/actors/actor/metadata/MessageMetadataProvider.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,67 @@ | ||
package io.appform.dropwizard.actors.actor.metadata; | ||
|
||
import com.rabbitmq.client.AMQP; | ||
import com.rabbitmq.client.Envelope; | ||
import io.appform.dropwizard.actors.actor.MessageMetadata; | ||
import io.appform.dropwizard.actors.common.RabbitmqActorException; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class MessageMetadataProvider { | ||
|
||
private final List<MessageMetadataGenerator> messageMetadataGenerators; | ||
|
||
public MessageMetadataProvider(List<String> messageMetaGeneratorClasses) { | ||
messageMetadataGenerators = configureMessageMetadataGenerators(messageMetaGeneratorClasses); | ||
} | ||
|
||
private List<MessageMetadataGenerator> configureMessageMetadataGenerators(List<String> messageMetaGeneratorClasses) { | ||
if (Objects.isNull(messageMetaGeneratorClasses)) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
return messageMetaGeneratorClasses.stream() | ||
.distinct() | ||
.map(className -> { | ||
try { | ||
@SuppressWarnings("unchecked") | ||
Class<MessageMetadataGenerator> clazz = (Class<MessageMetadataGenerator>) Class.forName( | ||
className); | ||
return clazz.getDeclaredConstructor().newInstance(); | ||
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | | ||
IllegalAccessException | InvocationTargetException e) { | ||
throw RabbitmqActorException.propagate("Failed to initialize messageMetadataGenerators", e); | ||
} | ||
}) | ||
.toList(); | ||
} | ||
|
||
public MessageMetadata createMetadata(final Envelope envelope, final AMQP.BasicProperties properties) { | ||
Objects.requireNonNull(envelope, "Envelope should not be null"); | ||
Objects.requireNonNull(properties, "AMQP.BasicProperties should not be null"); | ||
MessageMetaContext messageMetaContext = MessageMetaContext.builder() | ||
.redelivered(envelope.isRedeliver()) | ||
.contentType(properties.getContentType()) | ||
.contentEncoding(properties.getContentEncoding()) | ||
.headers(properties.getHeaders()) | ||
.deliveryMode(properties.getDeliveryMode()) | ||
.priority(properties.getPriority()) | ||
.correlationId(properties.getCorrelationId()) | ||
.replyTo(properties.getReplyTo()) | ||
.expiration(properties.getExpiration()) | ||
.messageId(properties.getMessageId()) | ||
.timestamp(properties.getTimestamp()) | ||
.type(properties.getType()) | ||
.userId(properties.getUserId()) | ||
.appId(properties.getAppId()) | ||
.clusterId(properties.getClusterId()) | ||
.build(); | ||
|
||
MessageMetadata messageMetadata = MessageMetadata.builder().build(); | ||
messageMetadataGenerators.forEach(populator -> populator.generate(messageMetaContext, messageMetadata)); | ||
return messageMetadata; | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...ain/java/io/appform/dropwizard/actors/actor/metadata/MessageReDeliveredMetaGenerator.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,12 @@ | ||
package io.appform.dropwizard.actors.actor.metadata; | ||
|
||
import io.appform.dropwizard.actors.actor.MessageMetadata; | ||
|
||
public class MessageReDeliveredMetaGenerator implements MessageMetadataGenerator { | ||
|
||
@Override | ||
public void generate(final MessageMetaContext messageMetaContext, MessageMetadata messageMetadata) { | ||
messageMetadata.setRedelivered(messageMetaContext.isRedelivered()); | ||
} | ||
|
||
} |
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.