Search in sources :

Example 1 with MessagingException

use of io.helidon.messaging.MessagingException 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 MessagingException

use of io.helidon.messaging.MessagingException in project helidon by oracle.

the class OutgoingJmsMessage method fromJmsMessage.

@SuppressWarnings("unchecked")
static <PAYLOAD> OutgoingJmsMessage<PAYLOAD> fromJmsMessage(jakarta.jms.Message jmsMessage) throws JMSException {
    OutgoingJmsMessage<PAYLOAD> msg = new OutgoingJmsMessage<>();
    msg.postProcess(m -> {
        Enumeration<String> e = jmsMessage.getPropertyNames();
        while (e.hasMoreElements()) {
            String key = e.nextElement();
            m.setObjectProperty(key, jmsMessage.getObjectProperty(key));
        }
        // MessageId and timestamp is deliberately omitted
        getAndSet("correlationId", jmsMessage::getJMSCorrelationID, m::setJMSCorrelationID);
        getAndSet("deliveryMode", jmsMessage::getJMSDeliveryMode, m::setJMSDeliveryMode);
        getAndSet("deliveryTime", jmsMessage::getJMSDeliveryTime, m::setJMSDeliveryTime);
        getAndSet("destination", jmsMessage::getJMSDestination, m::setJMSDestination);
        getAndSet("expiration", jmsMessage::getJMSExpiration, m::setJMSExpiration);
        getAndSet("priority", jmsMessage::getJMSPriority, m::setJMSPriority);
        getAndSet("redelivered", jmsMessage::getJMSRedelivered, m::setJMSRedelivered);
        getAndSet("replyTo", jmsMessage::getJMSReplyTo, m::setJMSReplyTo);
        getAndSet("type", jmsMessage::getJMSType, m::setJMSType);
    });
    msg.onAck(() -> {
        try {
            jmsMessage.acknowledge();
            return CompletableFuture.completedFuture(null);
        } catch (IllegalStateException e) {
            // deliberately noop, original's jms session is closed
            return CompletableFuture.completedFuture(null);
        } catch (JMSException e) {
            throw new MessagingException("Error when acking original jakarta.jms.Message");
        }
    });
    return msg;
}
Also used : MessagingException(io.helidon.messaging.MessagingException) JMSException(jakarta.jms.JMSException)

Example 3 with MessagingException

use of io.helidon.messaging.MessagingException in project helidon by oracle.

the class AqConnectorImpl method createAqFactory.

private AQjmsConnectionFactory createAqFactory(Config c) throws javax.jms.JMSException {
    ConfigValue<String> user = c.get(USERNAME_ATTRIBUTE).asString();
    ConfigValue<String> password = c.get(PASSWORD_ATTRIBUTE).asString();
    ConfigValue<String> url = c.get(URL_ATTRIBUTE).asString();
    ConfigValue<String> dataSourceName = c.get(DATASOURCE_ATTRIBUTE).asString();
    AQjmsConnectionFactory fact = new AQjmsConnectionFactory();
    if (dataSourceName.isPresent()) {
        if (user.isPresent()) {
            throw new MessagingException("When " + DATASOURCE_ATTRIBUTE + " is set, properties " + String.join(", ", USERNAME_ATTRIBUTE, PASSWORD_ATTRIBUTE, URL_ATTRIBUTE) + " are forbidden!");
        }
        // DataSource provided via SE builder
        DataSource seDataSource = dataSourceMap.get(dataSourceName.get());
        if (seDataSource != null) {
            fact.setDatasource(seDataSource);
        }
        // DataSource provided via CDI as named bean
        if (IS_CDI.get()) {
            Instance<DataSource> dataSources = CDI.current().select(DataSource.class, NamedLiteral.of(dataSourceName.get()));
            if (dataSources.isResolvable()) {
                fact.setDatasource(dataSources.get());
            } else {
                throw new MessagingException("Datasource " + dataSourceName.get() + (dataSources.isAmbiguous() ? " is ambiguous!" : " not found!"));
            }
        }
    }
    if (url.isPresent()) {
        fact.setJdbcURL(url.get());
    }
    if (user.isPresent()) {
        fact.setUsername(user.get());
    }
    if (password.isPresent()) {
        fact.setPassword(password.get());
    }
    return fact;
}
Also used : AQjmsConnectionFactory(oracle.jms.AQjmsConnectionFactory) MessagingException(io.helidon.messaging.MessagingException) DataSource(javax.sql.DataSource)

Example 4 with MessagingException

use of io.helidon.messaging.MessagingException in project helidon by oracle.

the class JmsBytesMessage method getPayload.

@Override
public byte[] getPayload() {
    try {
        byte[] bytes = new byte[(int) msg.getBodyLength()];
        msg.readBytes(bytes);
        return bytes;
    } catch (JMSException e) {
        throw new MessagingException("Error when reading BytesMessage", e);
    }
}
Also used : MessagingException(io.helidon.messaging.MessagingException) JMSException(jakarta.jms.JMSException)

Example 5 with MessagingException

use of io.helidon.messaging.MessagingException in project helidon by oracle.

the class JmsConnector method getPublisherBuilder.

@Override
public PublisherBuilder<? extends Message<?>> getPublisherBuilder(Config mpConfig) {
    io.helidon.config.Config config = MpConfig.toHelidonConfig(mpConfig);
    AcknowledgeMode ackMode = config.get(ACK_MODE_ATTRIBUTE).asString().map(AcknowledgeMode::parse).orElse(ACK_MODE_DEFAULT);
    Boolean awaitAck = config.get(AWAIT_ACK_ATTRIBUTE).asBoolean().orElse(AWAIT_ACK_DEFAULT);
    ConnectionContext ctx = new ConnectionContext(config);
    ConnectionFactory factory = getFactory(ctx).orElseThrow(() -> new MessagingException("No ConnectionFactory found."));
    try {
        SessionMetadata sessionEntry = prepareSession(config, factory);
        Destination destination = createDestination(sessionEntry.session(), ctx);
        String messageSelector = config.get(MESSAGE_SELECTOR_ATTRIBUTE).asString().orElse(null);
        String subscriberName = config.get(SUBSCRIBER_NAME_ATTRIBUTE).asString().orElse(null);
        MessageConsumer consumer;
        if (config.get(DURABLE_ATTRIBUTE).asBoolean().orElse(false)) {
            if (!(destination instanceof Topic)) {
                throw new MessagingException("Can't create durable consumer. Only topic can be durable!");
            }
            consumer = sessionEntry.session().createDurableSubscriber((Topic) destination, subscriberName, messageSelector, config.get(NON_LOCAL_ATTRIBUTE).asBoolean().orElse(false));
        } else {
            consumer = sessionEntry.session().createConsumer(destination, messageSelector);
        }
        BufferedEmittingPublisher<Message<?>> emitter = BufferedEmittingPublisher.create();
        Long pollTimeout = config.get(POLL_TIMEOUT_ATTRIBUTE).asLong().orElse(POLL_TIMEOUT_DEFAULT);
        AtomicReference<JmsMessage<?>> lastMessage = new AtomicReference<>();
        scheduler.scheduleAtFixedRate(() -> produce(emitter, sessionEntry, consumer, ackMode, awaitAck, pollTimeout, lastMessage), 0, config.get(PERIOD_EXECUTIONS_ATTRIBUTE).asLong().orElse(PERIOD_EXECUTIONS_DEFAULT), TimeUnit.MILLISECONDS);
        sessionEntry.connection().start();
        return ReactiveStreams.fromPublisher(FlowAdapters.toPublisher(Multi.create(emitter)));
    } catch (JMSException e) {
        LOGGER.log(Level.SEVERE, e, () -> "Error during JMS publisher preparation");
        return ReactiveStreams.failed(e);
    }
}
Also used : Destination(jakarta.jms.Destination) MessageConsumer(jakarta.jms.MessageConsumer) BytesMessage(jakarta.jms.BytesMessage) Message(org.eclipse.microprofile.reactive.messaging.Message) TextMessage(jakarta.jms.TextMessage) MessagingException(io.helidon.messaging.MessagingException) AtomicReference(java.util.concurrent.atomic.AtomicReference) JMSException(jakarta.jms.JMSException) ConnectionFactory(jakarta.jms.ConnectionFactory) Topic(jakarta.jms.Topic)

Aggregations

MessagingException (io.helidon.messaging.MessagingException)6 JMSException (jakarta.jms.JMSException)5 BytesMessage (jakarta.jms.BytesMessage)2 ConnectionFactory (jakarta.jms.ConnectionFactory)2 Destination (jakarta.jms.Destination)2 MessageConsumer (jakarta.jms.MessageConsumer)2 TextMessage (jakarta.jms.TextMessage)2 Topic (jakarta.jms.Topic)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Builder (io.helidon.common.Builder)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 ConfigValue (io.helidon.config.ConfigValue)1 MpConfig (io.helidon.config.mp.MpConfig)1 Stoppable (io.helidon.messaging.Stoppable)1 ApplicationScoped (jakarta.enterprise.context.ApplicationScoped)1 BeforeDestroyed (jakarta.enterprise.context.BeforeDestroyed)1 Observes (jakarta.enterprise.event.Observes)1