com.sap.cds.feature.messaging.mq.service.MessageQueuingService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cds-feature-message-queuing Show documentation
Show all versions of cds-feature-message-queuing Show documentation
Message Queuing feature for CDS Services Java
The newest version!
/**************************************************************************
* (C) 2019-2024 SAP SE or an SAP affiliate company. All rights reserved. *
**************************************************************************/
package com.sap.cds.feature.messaging.mq.service;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import org.apache.qpid.jms.message.JmsBytesMessage;
import org.apache.qpid.jms.message.JmsTextMessage;
import org.apache.qpid.jms.provider.amqp.message.AmqpJmsBytesMessageFacade;
import org.apache.qpid.jms.provider.amqp.message.AmqpJmsTextMessageFacade;
import com.sap.cds.feature.messaging.mq.client.MessageQueuingManagementClient;
import com.sap.cds.feature.messaging.mq.jms.MessageQueuingConnectionProvider;
import com.sap.cds.services.environment.CdsProperties.Messaging.MessagingServiceConfig;
import com.sap.cds.services.messaging.TopicMessageEventContext;
import com.sap.cds.services.messaging.jms.BrokerConnection;
import com.sap.cds.services.messaging.service.AbstractMessagingService;
import com.sap.cds.services.messaging.service.MessagingBrokerQueueListener;
import com.sap.cds.services.runtime.CdsRuntime;
import com.sap.cloud.environment.servicebinding.api.ServiceBinding;
import jakarta.jms.BytesMessage;
import jakarta.jms.JMSException;
import jakarta.jms.Message;
/**
* Implementation of the enterprise messaging connector.
*/
public class MessageQueuingService extends AbstractMessagingService {
private final MessageQueuingConnectionProvider connectionProvider;
private final MessageQueuingManagementClient managementClient;
private volatile BrokerConnection connection;
public MessageQueuingService(MessagingServiceConfig serviceConfig, ServiceBinding binding, MessageQueuingConnectionProvider connectionProvider, CdsRuntime runtime) {
super(serviceConfig, runtime);
this.connectionProvider = connectionProvider;
this.managementClient = new MessageQueuingManagementClient(binding);
}
@Override
public void init() {
// create the broker connection asynchronously with topic subscriptions
connectionProvider.asyncConnectionInitialization(serviceConfig, connection -> {
this.connection = connection;
super.init();
});
}
@Override
public void stop() {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
// ignored
}
}
}
@Override
protected String toFullyQualifiedTopicName(String event, boolean inbound) {
return event.replace('.', '/');
}
@Override
protected void removeQueue(String name) throws IOException {
managementClient.removeQueue(name);
}
@Override
protected void createQueue(String name, Map properties) throws IOException {
if (properties.containsKey("deadMessageQueue")) {
String dmQueue = (String) properties.get("deadMessageQueue");
if (managementClient.getQueue(dmQueue) == null) {
managementClient.createQueue(dmQueue, Collections.emptyMap());
}
}
managementClient.createQueue(name, properties);
}
@Override
protected void createQueueSubscription(String queue, String topic) throws IOException {
managementClient.createQueueSubscription(queue, topic);
}
@Override
protected void registerQueueListener(String queue, MessagingBrokerQueueListener listener) throws IOException {
connection.registerQueueListener(queue, listener, m -> getMessageTopic(m));
}
@Override
protected void emitTopicMessage(String topic, TopicMessageEventContext messageEventContext) {
connection.emitTopicMessage(topic, messageEventContext);
}
private String getMessageTopic(Message message) {
if (message instanceof JmsTextMessage textMessage) {
if (textMessage.getFacade() instanceof AmqpJmsTextMessageFacade) {
AmqpJmsTextMessageFacade amqpMessage = (AmqpJmsTextMessageFacade) textMessage.getFacade();
return amqpMessage.getDestination().getAddress();
}
} else if (message instanceof BytesMessage) {
if (((JmsBytesMessage) message).getFacade() instanceof AmqpJmsBytesMessageFacade) {
AmqpJmsBytesMessageFacade amqpMessage = (AmqpJmsBytesMessageFacade) ((JmsBytesMessage) message).getFacade();
return amqpMessage.getDestination().getAddress();
}
}
return null;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy