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);
}
}
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;
}
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;
}
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);
}
}
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);
}
}
Aggregations