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
/**************************************************************************
* (C) 2019-2021 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.Map;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.Message;
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.MessageQueuingManagmentClient;
import com.sap.cds.feature.messaging.mq.jms.MessageQueuingConnectionProvider;
import com.sap.cds.services.environment.CdsProperties.Messaging.MessagingServiceConfig;
import com.sap.cds.services.environment.ServiceBinding;
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;
/**
* Implementation of the enterprise messaging connector.
*/
public class MessageQueuingService extends AbstractMessagingService {
private final MessageQueuingConnectionProvider connectionProvider;
private final MessageQueuingManagmentClient managementClient;
private final int maxFailedAttempts;
private volatile BrokerConnection connection;
@SuppressWarnings("deprecation")
public MessageQueuingService(MessagingServiceConfig serviceConfig, ServiceBinding binding, MessageQueuingConnectionProvider connectionProvider, CdsRuntime runtime) {
super(serviceConfig, runtime);
this.connectionProvider = connectionProvider;
this.managementClient = new MessageQueuingManagmentClient(binding);
this.maxFailedAttempts = serviceConfig.getQueue().getMaxFailedAttempts();
}
@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 {
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), maxFailedAttempts);
}
@Override
protected void emitTopicMessage(String topic, TopicMessageEventContext messageEventContext) {
connection.emitTopicMessage(topic, messageEventContext);
}
private String getMessageTopic(Message message) {
if (message instanceof JmsTextMessage) {
if (((JmsTextMessage) message).getFacade() instanceof AmqpJmsTextMessageFacade) {
AmqpJmsTextMessageFacade amqpMessage = (AmqpJmsTextMessageFacade) ((JmsTextMessage) message).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 - 2025 Weber Informatics LLC | Privacy Policy