microsoft / azure-spring-boot

Spring Boot Starters for Azure services
MIT License
374 stars 460 forks source link

Cannot perform operation 'sendMessage' on a disposed sender. #956

Closed anshul733 closed 1 year ago

anshul733 commented 1 year ago

In our Spring boot Java app flow, we were not able to send message using ASB sdk to a topic. We also did not see any retries while publishing the message.

Here is the stack trace

Caused by: java.lang.IllegalStateException: Cannot perform operation 'sendMessage' on a disposed sender.
    at com.azure.messaging.servicebus.ServiceBusSenderAsyncClient.sendInternal(ServiceBusSenderAsyncClient.java:798)
    at com.azure.messaging.servicebus.ServiceBusSenderAsyncClient.sendMessage(ServiceBusSenderAsyncClient.java:237)
    at com.azure.messaging.servicebus.ServiceBusSenderClient.sendMessage(ServiceBusSenderClient.java:183)
    at 
com.fmr.prdc.postprice.recon.infrastructure.adapter.IncompleteTransactionEventPublisherAdapter.publishIncompleteTransactionEvent(IncompleteTransactionEventPublisherAdapter.java:61)
    at com.fmr.prdc.postprice.recon.core.facade.BaseReconEventHandler.notifyIncompleteTransactionProcessor(BaseReconEventHandler.java:545)
    at com.fmr.prdc.postprice.recon.core.facade.ContributionActivityDefaultReconHandler.handleReconSummaryEvent(ContributionActivityDefaultReconHandler.java:84)
    at com.fmr.prdc.postprice.recon.core.facade.LoanRepaymentsDefaultReconHandler.handleReconSummaryEvent(LoanRepaymentsDefaultReconHandler.java:61)
    at com.fmr.prdc.postprice.recon.application.consumer.ReconSetupListener.processMessage(ReconSetupListener.java:95)
    at com.fmr.prdc.postprice.recon.application.consumer.ReconSetupListener.handleReconEvent(ReconSetupListener.java:59)
    at jdk.internal.reflect.GeneratedMethodAccessor226.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:169)
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:119)
    at org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:114)
    ... 10 common frames omitted

Here is the SDK version we use

<dependency>
                <groupId>com.azure</groupId>
                <artifactId>azure-messaging-servicebus</artifactId>
                <version>7.10.0</version>
</dependency>

JDK version - 11

Spring boot version - 2.6.6

Here is the code snippet we use to publsh the message

package com.fmr.prdc.postprice.recon.infrastructure.adapter;

import com.azure.messaging.servicebus.ServiceBusMessage;
import com.azure.messaging.servicebus.ServiceBusSenderClient;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fmr.prdc.constants.IncompleteTransactionProcessMsdIds;
import com.fmr.prdc.constants.JmsHeaderConstants;
import com.fmr.prdc.contracts.IncompleteTransactionEvent;
import com.fmr.prdc.contracts.RRKEvent;
import com.fmr.prdc.postprice.recon.core.ports.outgoing.IncompleteTransactionEventPublisher;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Component
@Slf4j
@RequiredArgsConstructor
public class IncompleteTransactionEventPublisherAdapter implements IncompleteTransactionEventPublisher {
    private final ObjectMapper objectMapper;
    private final ServiceBusSenderClient incompleteTransactionEventClient;
    public static final String APPLICATION_JSON_VALUE = "application/json";

    @Override
    public void publishIncompleteTransactionEvent(IncompleteTransactionEvent incompleteTransactionEvent, IncompleteTransactionProcessMsdIds incompleteTransactionProcessMsdIds) {

        var trackingId = UUID.randomUUID().toString();
        var eventAction = incompleteTransactionProcessMsdIds.toString();
        var eventRoutingType = "INCP_" + incompleteTransactionProcessMsdIds.getIncpTxnMsgId();
        var msdId = incompleteTransactionProcessMsdIds.toString();

        // Adding Headers
        Map<String, String> headers = new HashMap<>();
        headers.putIfAbsent(JmsHeaderConstants.SOURCE_DOMAIN, "AP140321");
        headers.putIfAbsent(JmsHeaderConstants.VERSION, "0.1");
        headers.putIfAbsent(
                JmsHeaderConstants.EVENT_ACTION, eventAction);
        headers.putIfAbsent(JmsHeaderConstants.TRACKING_ID, trackingId);

        // Creating New Event
        var rrkEvent = RRKEvent.builder().headers(headers).payload(incompleteTransactionEvent).build();
        ServiceBusMessage serviceBusMessage;

        try {
            serviceBusMessage = new ServiceBusMessage(
                    objectMapper.writeValueAsString(rrkEvent));
        } catch (JsonProcessingException jsonProcessingException) {
            log.error("JsonProcessingException :", jsonProcessingException);
            throw new RuntimeException("Unable to publish incompleteTransactionEvent");
        }
        Map<String, Object> appProperties =  serviceBusMessage.getApplicationProperties();
        appProperties.put("reprocessEventRoutingType", eventRoutingType);
        serviceBusMessage
                .setContentType(APPLICATION_JSON_VALUE)
                .setMessageId(msdId);
        incompleteTransactionEventClient.sendMessage(serviceBusMessage);
        log.info("Published {} Event = {} TrackingId = {}", eventAction, rrkEvent, trackingId);

    }
}
yiliuTo commented 1 year ago

Hi @anshul733 , thanks for reaching out. This repository has been deprecated and migrated to https://github.com/Azure/azure-sdk-for-java. So I will close this one and create another there to track your issue.