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; }
private TestHandler getTestHandler(PartitionProcessor processor) { return (TestHandler) processor.getTypeDictionary().messageHandlerFor(null); }
@Test public void testCreateFromClasspath() { // The ReflectionTypeDictionaryFactory is robust by design, so it does not throw any Exception. // Need to check the log output for the error test cases. // Dependency injection magic ServiceProperties serviceProperites = new ServiceProperties(); Module[] modules = new Module[1]; modules[0] = new TestInjectionModule(serviceProperites); Injector injector = Guice.createInjector(modules); ReflectionTypeDictionaryFactory rtdf = new ReflectionTypeDictionaryFactory(injector); TypeDictionary dictionary = rtdf.createFromClasspath(); MessageHandler<? extends com.google.protobuf.Message> handler = dictionary.messageHandlerFor(MessageType.of(TypeDictionaryTest.class)); assertNotNull(handler); MessageHandler<? extends com.google.protobuf.Message> unknownHandler = dictionary.messageHandlerFor(MessageType.of(TestMessageWithNoHandler.class)); assertNull(unknownHandler); Parser parser = dictionary.parserFor((MessageType.of(TypeDictionaryTest.class))); assertNotNull(parser); assertNull(dictionary.parserFor(new MessageType("UnknownType"))); }
public TypeDictionary createFromClasspath() { logger.info("Creating TypeDictionary using reflection from standard classpath."); return new TypeDictionary(populateHandlersFromClasspath(), populateParsersFromClasspath()); }
private Message<? extends com.google.protobuf.Message> parseMessage() { Envelope envelope = null; try { envelope = Envelope.parseFrom(record.value()); } catch (InvalidProtocolBufferException parseError) { markAsConsumed(record.offset()); parsingFailed(envelope, parseError); return null; } try { MessageType type = new MessageType(envelope.getMessageType()); Parser<com.google.protobuf.Message> parser = typeDictionary.parserFor(type); if (parser == null) { throw new UnknownMessageTypeException(type); } com.google.protobuf.Message innerMessage = parser.parseFrom(envelope.getInnerMessage()); return Messages.fromKafka(innerMessage, envelope, record); } catch (InvalidProtocolBufferException | UnknownMessageTypeException unrecoverableParsingError) { markAsConsumed(record.offset()); parsingFailed(envelope, unrecoverableParsingError); return null; } }
private PartitionProcessorFactory processorFactory() { TypeDictionary typeDictionary = new TypeDictionary(new HashMap<>(), new HashMap<>()); FailedMessageProcessor failedMessageProcessor = new DiscardFailedMessages(); return new PartitionProcessorFactory(typeDictionary, failedMessageProcessor, null, null); }
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; } }
@SuppressWarnings("unchecked") private void deliverToMessageHandler(Message message) { boolean tryDeliverMessage = true; boolean deliveryFailed = true; OrangeContext context = message.getMetadata().newContextFromMetadata(); try { while (tryDeliverMessage) { try { MessageType messageType = message.getMetadata().getType(); MessageHandler handler = typeDictionary.messageHandlerFor(messageType); if (handler == null) { throw new UnknownMessageHandlerException(messageType); } deliveryStarted(message, handler, context); // Leave the framework here: hand over execution to service-specific handler. handler.onMessage(message, context); deliveryFailed = false; break; } catch (Exception failure) { // Strategy decides: Should we retry to deliver the failed message? tryDeliverMessage = failedMessageProcessor.onFailedMessage(message, failure); deliveryFailed(message, failure, tryDeliverMessage); } } } finally { // consume the message - even if delivery failed markAsConsumed(message.getMetadata().getOffset()); deliveryEnded(message, deliveryFailed); } }
final CountDownLatch responseLatch = new CountDownLatch(N); TypeDictionary typeDictionary = new TypeDictionary(); ReflectionTypeDictionaryFactory typeDictionaryFactory = new ReflectionTypeDictionaryFactory(null); typeDictionary.putAllParsers(typeDictionaryFactory.populateParsersFromClasspath()); typeDictionary.putHandler(MessageType.of(SayHelloToCmd.class), new MessageHandler<SayHelloToCmd>() { @Override public void onMessage(Message<SayHelloToCmd> message, OrangeContext context) { typeDictionary.putHandler(MessageType.of(SayHelloToReply.class), new MessageHandler<SayHelloToReply>() { @Override public void onMessage(Message<SayHelloToReply> message, OrangeContext context) {