Search in sources :

Example 1 with Builder

use of io.helidon.common.Builder in project helidon by oracle.

the class JmsConnector method getSubscriberBuilder.

@Override
public SubscriberBuilder<? extends Message<?>, Void> getSubscriberBuilder(Config mpConfig) {
    io.helidon.config.Config config = MpConfig.toHelidonConfig(mpConfig);
    ConnectionContext ctx = new ConnectionContext(config);
    ConnectionFactory factory = getFactory(ctx).orElseThrow(() -> new MessagingException("No ConnectionFactory found."));
    try {
        SessionMetadata sessionEntry = prepareSession(config, factory);
        Session session = sessionEntry.session();
        Destination destination = createDestination(session, ctx);
        MessageProducer producer = session.createProducer(destination);
        AtomicReference<MessageMappers.MessageMapper> mapper = new AtomicReference<>();
        return ReactiveStreams.<Message<?>>builder().flatMapCompletionStage(m -> consume(m, session, mapper, producer, config)).onError(t -> LOGGER.log(Level.SEVERE, t, () -> "Error intercepted from channel " + config.get(CHANNEL_NAME_ATTRIBUTE).asString().orElse("unknown"))).ignore();
    } catch (JMSException e) {
        throw new MessagingException("Error when creating JMS producer.", e);
    }
}
Also used : BufferedEmittingPublisher(io.helidon.common.reactive.BufferedEmittingPublisher) OutgoingConnectorFactory(org.eclipse.microprofile.reactive.messaging.spi.OutgoingConnectorFactory) Observes(jakarta.enterprise.event.Observes) BytesMessage(jakarta.jms.BytesMessage) MpConfig(io.helidon.config.mp.MpConfig) MessageProducer(jakarta.jms.MessageProducer) Builder(io.helidon.common.Builder) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) JMSException(jakarta.jms.JMSException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Instance(jakarta.enterprise.inject.Instance) Session(jakarta.jms.Session) Level(java.util.logging.Level) FlowAdapters(org.reactivestreams.FlowAdapters) MessageConsumer(jakarta.jms.MessageConsumer) Map(java.util.Map) Destination(jakarta.jms.Destination) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) BiConsumer(java.util.function.BiConsumer) Connector(org.eclipse.microprofile.reactive.messaging.spi.Connector) ConnectionFactory(jakarta.jms.ConnectionFactory) Connection(jakarta.jms.Connection) IncomingConnectorFactory(org.eclipse.microprofile.reactive.messaging.spi.IncomingConnectorFactory) MessagingException(io.helidon.messaging.MessagingException) ReactiveStreams(org.eclipse.microprofile.reactive.streams.operators.ReactiveStreams) ExecutorService(java.util.concurrent.ExecutorService) Multi(io.helidon.common.reactive.Multi) Executor(java.util.concurrent.Executor) ScheduledThreadPoolSupplier(io.helidon.common.configurable.ScheduledThreadPoolSupplier) NamedLiteral(jakarta.enterprise.inject.literal.NamedLiteral) SubscriberBuilder(org.eclipse.microprofile.reactive.streams.operators.SubscriberBuilder) UUID(java.util.UUID) ConfigValue(io.helidon.config.ConfigValue) Stoppable(io.helidon.messaging.Stoppable) Config(org.eclipse.microprofile.config.Config) Logger(java.util.logging.Logger) BeforeDestroyed(jakarta.enterprise.context.BeforeDestroyed) TimeUnit(java.util.concurrent.TimeUnit) Message(org.eclipse.microprofile.reactive.messaging.Message) CompletionStage(java.util.concurrent.CompletionStage) Topic(jakarta.jms.Topic) TextMessage(jakarta.jms.TextMessage) Optional(java.util.Optional) Inject(jakarta.inject.Inject) ThreadPoolSupplier(io.helidon.common.configurable.ThreadPoolSupplier) PublisherBuilder(org.eclipse.microprofile.reactive.streams.operators.PublisherBuilder) Destination(jakarta.jms.Destination) MessagingException(io.helidon.messaging.MessagingException) AtomicReference(java.util.concurrent.atomic.AtomicReference) JMSException(jakarta.jms.JMSException) ConnectionFactory(jakarta.jms.ConnectionFactory) MessageProducer(jakarta.jms.MessageProducer) Session(jakarta.jms.Session)

Example 2 with Builder

use of io.helidon.common.Builder in project helidon by oracle.

the class GrpcClientBuilder method addServiceMethod.

/**
 * Add a method to the {@link ClientServiceDescriptor.Builder}.
 * <p>
 * The method configuration will be determined by the annotations present on the
 * method and the method signature.
 *
 * @param builder  the {@link ClientServiceDescriptor.Builder} to add the method to
 * @param method   the {@link io.helidon.microprofile.grpc.core.AnnotatedMethod} representing the method to add
 */
private void addServiceMethod(ClientServiceDescriptor.Builder builder, AnnotatedMethod method) {
    GrpcMethod annotation = method.firstAnnotationOrMetaAnnotation(GrpcMethod.class);
    String name = determineMethodName(method, annotation);
    MethodHandler handler = handlerSuppliers().stream().filter(supplier -> supplier.supplies(method)).findFirst().map(supplier -> supplier.get(name, method, instanceSupplier())).orElseThrow(() -> new IllegalArgumentException("Cannot locate a method handler supplier for method " + method));
    Class<?> requestType = handler.getRequestType();
    Class<?> responseType = handler.getResponseType();
    AnnotatedMethodConfigurer configurer = new AnnotatedMethodConfigurer(method, requestType, responseType, handler);
    switch(annotation.type()) {
        case UNARY:
            builder.unary(name, configurer);
            break;
        case CLIENT_STREAMING:
            builder.clientStreaming(name, configurer);
            break;
        case SERVER_STREAMING:
            builder.serverStreaming(name, configurer);
            break;
        case BIDI_STREAMING:
            builder.bidirectional(name, configurer);
            break;
        case UNKNOWN:
        default:
            LOGGER.log(Level.SEVERE, () -> "Unrecognized method type " + annotation.type());
    }
}
Also used : GrpcMethod(io.helidon.microprofile.grpc.core.GrpcMethod) GrpcMethod(io.helidon.microprofile.grpc.core.GrpcMethod) Builder(io.helidon.common.Builder) MethodHandler(io.helidon.grpc.core.MethodHandler) AnnotatedMethod(io.helidon.microprofile.grpc.core.AnnotatedMethod) GrpcMarshaller(io.helidon.microprofile.grpc.core.GrpcMarshaller) AnnotatedMethodList(io.helidon.microprofile.grpc.core.AnnotatedMethodList) Logger(java.util.logging.Logger) Supplier(java.util.function.Supplier) ClientServiceDescriptor(io.helidon.grpc.client.ClientServiceDescriptor) ModelHelper(io.helidon.microprofile.grpc.core.ModelHelper) Level(java.util.logging.Level) Objects(java.util.Objects) Consumer(java.util.function.Consumer) ClientMethodDescriptor(io.helidon.grpc.client.ClientMethodDescriptor) Instance(io.helidon.microprofile.grpc.core.Instance) AbstractServiceBuilder(io.helidon.microprofile.grpc.core.AbstractServiceBuilder) MethodHandler(io.helidon.grpc.core.MethodHandler)

Example 3 with Builder

use of io.helidon.common.Builder in project helidon by oracle.

the class GrpcServiceBuilder method addServiceMethod.

/**
 * Add a method to the {@link ServiceDescriptor.Builder}.
 * <p>
 * The method configuration will be determined by the annotations present on the
 * method and the method signature.
 *
 * @param builder  the {@link ServiceDescriptor.Builder} to add the method to
 * @param method   the {@link AnnotatedMethod} representing the method to add
 * @param beanManager  the {@link jakarta.enterprise.inject.spi.BeanManager} to use
 *                     to look-up CDI beans.
 */
@SuppressWarnings("unchecked")
private void addServiceMethod(ServiceDescriptor.Builder builder, AnnotatedMethod method, BeanManager beanManager) {
    GrpcMethod annotation = method.firstAnnotationOrMetaAnnotation(GrpcMethod.class);
    String name = determineMethodName(method, annotation);
    Supplier<?> instanceSupplier = instanceSupplier();
    MethodHandler handler = handlerSuppliers().stream().filter(supplier -> supplier.supplies(method)).findFirst().map(supplier -> supplier.get(name, method, instanceSupplier)).orElseThrow(() -> new IllegalArgumentException("Cannot locate a method handler supplier for method " + method));
    Class<?> requestType = handler.getRequestType();
    Class<?> responseType = handler.getResponseType();
    List<ServerInterceptor> interceptors = lookupMethodInterceptors(beanManager, method);
    GrpcInterceptors grpcInterceptors = method.getAnnotation(GrpcInterceptors.class);
    if (grpcInterceptors != null) {
        for (Class<?> interceptorClass : grpcInterceptors.value()) {
            ServerInterceptor interceptor = resolveInterceptor(beanManager, interceptorClass);
            if (interceptor != null) {
                interceptors.add(interceptor);
            }
        }
    }
    AnnotatedMethodConfigurer configurer = new AnnotatedMethodConfigurer(method, requestType, responseType, interceptors);
    switch(annotation.type()) {
        case UNARY:
            builder.unary(name, handler, configurer);
            break;
        case CLIENT_STREAMING:
            builder.clientStreaming(name, handler, configurer);
            break;
        case SERVER_STREAMING:
            builder.serverStreaming(name, handler, configurer);
            break;
        case BIDI_STREAMING:
            builder.bidirectional(name, handler, configurer);
            break;
        case UNKNOWN:
        default:
            LOGGER.log(Level.SEVERE, () -> "Unrecognized method type " + annotation.type());
    }
}
Also used : GrpcMethod(io.helidon.microprofile.grpc.core.GrpcMethod) Any(jakarta.enterprise.inject.Any) GrpcMethod(io.helidon.microprofile.grpc.core.GrpcMethod) Arrays(java.util.Arrays) Builder(io.helidon.common.Builder) GrpcInterceptors(io.helidon.microprofile.grpc.core.GrpcInterceptors) GrpcMarshaller(io.helidon.microprofile.grpc.core.GrpcMarshaller) AnnotatedMethodList(io.helidon.microprofile.grpc.core.AnnotatedMethodList) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) ServerInterceptor(io.grpc.ServerInterceptor) ContextKeys(io.helidon.grpc.core.ContextKeys) GrpcInterceptor(io.helidon.microprofile.grpc.core.GrpcInterceptor) BeanManager(jakarta.enterprise.inject.spi.BeanManager) GrpcInterceptorBinding(io.helidon.microprofile.grpc.core.GrpcInterceptorBinding) MethodDescriptor(io.helidon.grpc.server.MethodDescriptor) ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) MethodHandler(io.helidon.grpc.core.MethodHandler) AnnotatedMethod(io.helidon.microprofile.grpc.core.AnnotatedMethod) ServiceLoader(java.util.ServiceLoader) HelidonServiceLoader(io.helidon.common.serviceloader.HelidonServiceLoader) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) ModelHelper(io.helidon.microprofile.grpc.core.ModelHelper) Objects(java.util.Objects) Consumer(java.util.function.Consumer) List(java.util.List) Annotation(java.lang.annotation.Annotation) Instance(io.helidon.microprofile.grpc.core.Instance) Collections(java.util.Collections) AbstractServiceBuilder(io.helidon.microprofile.grpc.core.AbstractServiceBuilder) MethodHandler(io.helidon.grpc.core.MethodHandler) GrpcInterceptors(io.helidon.microprofile.grpc.core.GrpcInterceptors) ServerInterceptor(io.grpc.ServerInterceptor)

Aggregations

Builder (io.helidon.common.Builder)3 Level (java.util.logging.Level)3 Logger (java.util.logging.Logger)3 MethodHandler (io.helidon.grpc.core.MethodHandler)2 AbstractServiceBuilder (io.helidon.microprofile.grpc.core.AbstractServiceBuilder)2 AnnotatedMethod (io.helidon.microprofile.grpc.core.AnnotatedMethod)2 AnnotatedMethodList (io.helidon.microprofile.grpc.core.AnnotatedMethodList)2 GrpcMarshaller (io.helidon.microprofile.grpc.core.GrpcMarshaller)2 GrpcMethod (io.helidon.microprofile.grpc.core.GrpcMethod)2 Instance (io.helidon.microprofile.grpc.core.Instance)2 ModelHelper (io.helidon.microprofile.grpc.core.ModelHelper)2 Objects (java.util.Objects)2 Consumer (java.util.function.Consumer)2 Supplier (java.util.function.Supplier)2 ServerInterceptor (io.grpc.ServerInterceptor)1 ScheduledThreadPoolSupplier (io.helidon.common.configurable.ScheduledThreadPoolSupplier)1 ThreadPoolSupplier (io.helidon.common.configurable.ThreadPoolSupplier)1 BufferedEmittingPublisher (io.helidon.common.reactive.BufferedEmittingPublisher)1 Multi (io.helidon.common.reactive.Multi)1 HelidonServiceLoader (io.helidon.common.serviceloader.HelidonServiceLoader)1