use of io.nats.client.Dispatcher in project micronaut-nats by micronaut-projects.
the class NatsConsumerAdvice method process.
@Override
public void process(BeanDefinition<?> beanDefinition, ExecutableMethod<?, ?> method) {
if (method.hasAnnotation(NatsListener.class)) {
AnnotationValue<Subject> subjectAnn = method.getAnnotation(Subject.class);
if (subjectAnn != null) {
String subject = subjectAnn.getRequiredValue(String.class);
String connectionName = method.findAnnotation(NatsConnection.class).flatMap(conn -> conn.get("connection", String.class)).orElse(NatsConnection.DEFAULT_CONNECTION);
io.micronaut.context.Qualifier<Object> qualifer = beanDefinition.getAnnotationTypeByStereotype("javax.inject.Qualifier").map(type -> Qualifiers.byAnnotation(beanDefinition, type)).orElse(null);
Class<Object> beanType = (Class<Object>) beanDefinition.getBeanType();
Class<?> returnTypeClass = method.getReturnType().getType();
boolean isVoid = returnTypeClass == Void.class || returnTypeClass == void.class;
Object bean = beanContext.findBean(beanType, qualifer).orElseThrow(() -> new MessageListenerException("Could not find the bean to execute the method " + method));
Connection connection = beanContext.getBean(Connection.class, Qualifiers.byName(connectionName));
DefaultExecutableBinder<Message> binder = new DefaultExecutableBinder<>();
Dispatcher ds = connection.createDispatcher(msg -> {
BoundExecutable boundExecutable = null;
try {
boundExecutable = binder.bind(method, binderRegistry, msg);
} catch (Throwable e) {
handleException(new NatsListenerException("An error occurred binding the message to the method", e, bean, msg));
}
if (boundExecutable != null) {
Object returnedValue = boundExecutable.invoke(bean);
if (!isVoid && StringUtils.isNotEmpty(msg.getReplyTo())) {
byte[] converted = null;
if (returnedValue != null) {
NatsMessageSerDes serDes = serDesRegistry.findSerdes(method.getReturnType().asArgument()).map(NatsMessageSerDes.class::cast).orElseThrow(() -> new NatsListenerException(String.format("Could not find a serializer for the body argument of type [%s]", returnedValue.getClass().getName()), bean, msg));
converted = serDes.serialize(returnedValue);
}
connection.publish(msg.getReplyTo(), converted);
}
}
});
Optional<String> queueOptional = subjectAnn.get("queue", String.class);
if (queueOptional.isPresent() && !queueOptional.get().isEmpty()) {
ds.subscribe(subject, queueOptional.get());
} else {
ds.subscribe(subject);
}
consumerDispatchers.put(ds, subject);
}
}
}
use of io.nats.client.Dispatcher in project micronaut-nats by micronaut-projects.
the class NatsConsumerAdvice method close.
@Override
public void close() {
final Iterator<Map.Entry<Dispatcher, String>> it = consumerDispatchers.entrySet().iterator();
while (it.hasNext()) {
final Map.Entry<Dispatcher, String> entry = it.next();
Dispatcher dispatcher = entry.getKey();
String subject = entry.getValue();
dispatcher.unsubscribe(subject);
if (!dispatcher.isActive()) {
it.remove();
}
}
}
use of io.nats.client.Dispatcher in project carbon-mediation by wso2.
the class TLSConnection method initializeConsumer.
/**
* Consume the message received and inject into the sequence.
*
* @param sequenceName the sequence to inject the message to.
*/
@Override
public void initializeConsumer(String sequenceName) throws IOException, InterruptedException {
if (createConnection()) {
Dispatcher dispatcher;
dispatcher = connection.createDispatcher(natsMessage -> {
if (natsMessage != null) {
String message = new String(natsMessage.getData(), StandardCharsets.UTF_8);
if (log.isDebugEnabled()) {
log.debug("Message Received to NATS Inbound EP: " + message);
}
injectHandler.invoke(message.getBytes(), sequenceName, natsMessage.getReplyTo(), connection);
} else {
if (log.isDebugEnabled()) {
log.debug("Message is null.");
}
}
});
String queueGroup = natsProperties.getProperty(NatsConstants.QUEUE_GROUP);
if (StringUtils.isNotEmpty(queueGroup)) {
dispatcher.subscribe(subject, queueGroup);
} else {
dispatcher.subscribe(subject);
}
}
}
Aggregations