Search in sources :

Example 1 with ConnectionTaskException

use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionTaskException in project open-smart-grid-platform by OSGP.

the class DlmsConnectionMessageProcessor method createAndHandleConnectionForDevice.

public void createAndHandleConnectionForDevice(final DlmsDevice device, final MessageMetadata messageMetadata, final Consumer<DlmsConnectionManager> taskForConnectionManager) throws OsgpException {
    Permit permit = null;
    if (this.throttlingClientConfig.clientEnabled()) {
        permit = this.throttlingClientConfig.throttlingClient().requestPermitUsingNetworkSegmentIfIdsAreAvailable(messageMetadata.getBaseTransceiverStationId(), messageMetadata.getCellId());
    } else {
        this.throttlingService.openConnection();
    }
    final DlmsMessageListener dlmsMessageListener = this.createMessageListenerForDeviceConnection(device, messageMetadata);
    try {
        this.dlmsConnectionHelper.createAndHandleConnectionForDevice(messageMetadata, device, dlmsMessageListener, permit, taskForConnectionManager);
    } catch (final ConnectionTaskException e) {
        /*
       * Exceptions thrown by the tasks working with the DlmsConnectionManager are wrapped in
       * ConnectionTaskException in the ThrowingConsumer.
       */
        throw e.getOsgpException();
    } catch (final Exception e) {
        /*
       * Throttling permit is released in this catch block to make sure that it is made available
       * again when there were problems setting up the connection and only then (exceptions thrown
       * by the tasks working with the DlmsConnectionManager are wrapped as ConnectionTaskException
       * and are caught in the previous catch block, so they don't end up here).
       *
       * If there are no problems in setting up the connection, releasing the throttling permit is
       * the responsibility of the tasks for the DlmsConnectionManager, as seen in
       * DeviceRequestMessageProcessor.processMessageTasks(), where
       * this.doConnectionPostProcessing() is called in a finally block.
       */
        if (this.throttlingClientConfig.clientEnabled()) {
            this.throttlingClientConfig.throttlingClient().releasePermit(permit);
        } else {
            this.throttlingService.closeConnection();
        }
        throw e;
    }
}
Also used : Permit(org.opensmartgridplatform.throttling.api.Permit) ConnectionTaskException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionTaskException) ConnectionTaskException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionTaskException) OsgpException(org.opensmartgridplatform.shared.exceptionhandling.OsgpException) JMSException(javax.jms.JMSException) NonRetryableException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.NonRetryableException) ProtocolAdapterException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException)

Aggregations

JMSException (javax.jms.JMSException)1 ConnectionTaskException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionTaskException)1 NonRetryableException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.NonRetryableException)1 ProtocolAdapterException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException)1 OsgpException (org.opensmartgridplatform.shared.exceptionhandling.OsgpException)1 Permit (org.opensmartgridplatform.throttling.api.Permit)1