public static Message<? extends com.google.protobuf.Message> requestFor(Topic target, Topic replyTo, String partitionKey, com.google.protobuf.Message protoPayloadMessage, OrangeContext context) { boolean wasReceived = false; String messageId = UUID.randomUUID().toString(); String correlationId = context.getCorrelationId(); MessageType type = MessageType.of(protoPayloadMessage); // Use default inbox for service. if (replyTo == null) { throw new IllegalArgumentException("replyTo required"); } String requestCorrelationId = ""; // not required Metadata meta = new Metadata(wasReceived, target, partitionKey, -1, -1, messageId, correlationId, requestCorrelationId, replyTo, type); return new Message<>(protoPayloadMessage, meta); }
public Map<MessageType, Parser<com.google.protobuf.Message>> populateParsersFromClasspath() { Map<MessageType, Parser<com.google.protobuf.Message>> parsers = new HashMap<>(); List<Class<? extends com.google.protobuf.GeneratedMessageV3>> foundProtoMessages = new ArrayList<>(); new FastClasspathScanner() .matchSubclassesOf(com.google.protobuf.GeneratedMessageV3.class, matchingClass -> foundProtoMessages.add(matchingClass)).scan(); // This algorithm adds parsers for all protobuf messages in the classpath including base types such as com.google.protobuf.DoubleValue. for (Class<? extends com.google.protobuf.GeneratedMessageV3> clazz : foundProtoMessages) { try { java.lang.reflect.Method method = clazz.getMethod("parser"); // static method, no arguments @SuppressWarnings("unchecked") Parser<com.google.protobuf.Message> parser = (Parser<com.google.protobuf.Message>) method.invoke(null, (Object[]) null); // static method, no arguments parsers.put(MessageType.of(clazz), parser); // too noisy: logger.debug("Added parser for protobuf type {}", clazz.getTypeName()); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ignored) { // too noisy: logger.debug("Ignoring protobuf type {} as we cannot invoke static method parse().", clazz.getTypeName()); } } return parsers; }
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); }
MessageType type = MessageType.of(t); MessageHandler<? extends com.google.protobuf.Message> handler = null;
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); }
@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"))); }
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 <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; } }
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); }
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) {