public Consumer consumerForTopic(Topic topic, DiscardFailedMessages failedMessageStrategy) { String consumerGroupId = defaultConsumerGroupId(topic); return new Consumer(topic, consumerGroupId, defaultKafkaConfig(), defaultPartitionProcessorFactory(failedMessageStrategy)); }
@Override public ConsumerFactory get() { if(typeDictionary == null) { // Thread safety: no need for volatile / synchronization - it doesn't hurt much if the TypeDictionary is created multiple times. ReflectionTypeDictionaryFactory dictionaryFactory = new ReflectionTypeDictionaryFactory(injector); typeDictionary = dictionaryFactory.createFromClasspath(); } return new ConsumerFactory(serviceProperties, typeDictionary, tracer, metricBuilderFactory); } }
public UnknownMessageHandlerException(MessageType type) { super(type.toString()); } }
private <T extends com.google.protobuf.Message> ConsumerFactory consumerFactoryWithHandler(ServiceProperties serviceProperties, Class<T> messageType, MessageHandler<T> handler) { TypeDictionary typeDictionary = new TypeDictionary(); ReflectionTypeDictionaryFactory reflectionCruft = new ReflectionTypeDictionaryFactory(null); typeDictionary.putAllParsers(reflectionCruft.populateParsersFromClasspath()); typeDictionary.putHandler(MessageType.of(messageType), handler); ConsumerFactory consumerFactory = new ConsumerFactory(serviceProperties, typeDictionary, null, null); return consumerFactory; }
public static Message<? extends com.google.protobuf.Message> replyTo(Message originalRequest, com.google.protobuf.Message protoPayloadMessage, OrangeContext context) { boolean wasReceived = false; // By default, return to sender topic using same partitioning scheme. Topic target = originalRequest.getMetadata().getReplyTo(); String partitionKey = originalRequest.getMetadata().getPartitioningKey(); String messageId = UUID.randomUUID().toString(); String correlationId = context.getCorrelationId(); String requestCorrelationId = originalRequest.getMetadata().getMessageId(); Topic replyTo = null; // not required MessageType type = MessageType.of(protoPayloadMessage); Metadata meta = new Metadata(wasReceived, target, partitionKey, -1, -1, messageId, correlationId, requestCorrelationId, replyTo, type); return new Message<>(protoPayloadMessage, meta); }
private ConsumerRecord<String, byte[]> simulateKafkaInTheLoop(Message message, long offset) { Envelope envelope = Messages.toKafka(message); return new ConsumerRecord<String, byte[]>(message.getMetadata().getTopic().toString(), PARTITION, offset, message.getMetadata().getPartitioningKey(), envelope.toByteArray()); }
@Override public void onMessage(Message<SayHelloToCmd> message, OrangeContext context) { SayHelloToReply greeting = SayHelloToReply.newBuilder().setGreeting("Hello to " + message.getPayload().getName()).build(); Message reply = Messages.replyTo(message, greeting, context); producer.send(reply); requestLatch.countDown(); } });
static Message<? extends com.google.protobuf.Message> fromKafka(com.google.protobuf.Message protoMessage, Envelope envelope, ConsumerRecord<String, byte[]> record) { boolean wasReceived = true; Topic topic = new Topic(record.topic()); String partitioningKey = record.key(); int partitionId = record.partition(); long offset = record.offset(); String messageId = envelope.getMessageId(); String correlationId = envelope.getCorrelationId(); MessageType type = MessageType.of(protoMessage); String requestCorrelationId = envelope.getRequestCorrelationId(); Topic replyTo = new Topic(envelope.getReplyTo()); Metadata meta = new Metadata(wasReceived, topic, partitioningKey, partitionId, offset, messageId, correlationId, requestCorrelationId, replyTo, type); return new Message<>(protoMessage, meta); }
private ConsumerRecord<String, byte[]> testRecordWithOffset(long offset) { Envelope.Builder envelope = Envelope.newBuilder(); envelope.setMessageId("cruft"); return new ConsumerRecord<String, byte[]>(TOPIC, PARTITION, offset, KEY, envelope.build().toByteArray()); }
private PartitionProcessorFactory processorFactory() { TypeDictionary typeDictionary = new TypeDictionary(new HashMap<>(), new HashMap<>()); FailedMessageProcessor failedMessageProcessor = new DiscardFailedMessages(); return new PartitionProcessorFactory(typeDictionary, failedMessageProcessor, null, null); }
public TypeDictionary createFromClasspath() { logger.info("Creating TypeDictionary using reflection from standard classpath."); return new TypeDictionary(populateHandlersFromClasspath(), populateParsersFromClasspath()); }
private PartitionProcessor givenAPartionProcessor() { TopicPartition topicKey = new TopicPartition(TOPIC, PARTITION); TypeDictionary typeDictionary = new TestTypeDictionary(); FailedMessageProcessor failedMessageProcessor = new DiscardFailedMessages(); MetricBuilderFactory metricsBuilderFactory = null; Tracer tracer = null; return new PartitionProcessor(topicKey, typeDictionary, failedMessageProcessor, tracer, metricsBuilderFactory); }
public static Message<? extends com.google.protobuf.Message> oneWayMessage(Topic target, String partitionKey, com.google.protobuf.Message protoPayloadMessage, OrangeContext context) { boolean wasReceived = false; String messageId = UUID.randomUUID().toString(); String correlationId = context.getCorrelationId(); Topic replyTo = null; // not required String requestCorrelationId = ""; // not required MessageType type = MessageType.of(protoPayloadMessage); Metadata meta = new Metadata(wasReceived, target, partitionKey, -1, -1, messageId, correlationId, requestCorrelationId, replyTo, type); return new Message<>(protoPayloadMessage, meta); }
@Override public boolean onFailedMessage(Message failed, Throwable failureCause) { logger.warn(failed.getMetadata().getLoggingMarker(), "Discarded failing message.", failureCause); return false; } }
private TestHandler getTestHandler(PartitionProcessor processor) { return (TestHandler) processor.getTypeDictionary().messageHandlerFor(null); }
private PartitionProcessorFactory defaultPartitionProcessorFactory(FailedMessageProcessor failedMessageStrategy) { PartitionProcessorFactory partitionProcessorFactory = new PartitionProcessorFactory(typeDictionary, failedMessageStrategy, tracer, metricBuilderFactory); return partitionProcessorFactory; }
public boolean isOutbound() { return !isInbound(); }
@Override public Parser parserFor(MessageType type) { return EmptyMessage.parser(); } }
private <T extends com.google.protobuf.Message> ConsumerFactory consumerFactoryWithHandler(ServiceProperties serviceProperties, Class<T> messageType, MessageHandler<T> handler) { TypeDictionary typeDictionary = new TypeDictionary(); ReflectionTypeDictionaryFactory reflectionCruft = new ReflectionTypeDictionaryFactory(null); typeDictionary.putAllParsers(reflectionCruft.populateParsersFromClasspath()); typeDictionary.putHandler(MessageType.of(messageType), handler); ConsumerFactory consumerFactory = new ConsumerFactory(serviceProperties, typeDictionary, null, null); return consumerFactory; } }
public Consumer defaultInboxConsumer(FailedMessageProcessor failedMessageStrategy) { String serviceName = serviceProperties.getServiceName(); Topic defaultInbox = Topic.defaultServiceInbox(serviceName); String consumerGroupId = defaultConsumerGroupId(defaultInbox); return new Consumer(defaultInbox, consumerGroupId, defaultKafkaConfig(), defaultPartitionProcessorFactory(failedMessageStrategy)); }