Search in sources :

Example 1 with AbstractMessageChannelMetrics

use of org.springframework.integration.support.management.AbstractMessageChannelMetrics in project spring-integration by spring-projects.

the class IntegrationManagementConfigurer method configureChannelMetrics.

@SuppressWarnings("unchecked")
private void configureChannelMetrics(String name, MessageChannelMetrics bean) {
    AbstractMessageChannelMetrics metrics;
    if (bean instanceof PollableChannelManagement) {
        metrics = this.metricsFactory.createPollableChannelMetrics(name);
    } else {
        metrics = this.metricsFactory.createChannelMetrics(name);
    }
    Assert.state(metrics != null, "'metrics' must not be null");
    ManagementOverrides overrides = bean.getOverrides();
    Boolean enabled = PatternMatchUtils.smartMatch(name, this.enabledCountsPatterns);
    if (enabled != null) {
        bean.setCountsEnabled(enabled);
    } else {
        if (!overrides.countsConfigured) {
            bean.setCountsEnabled(this.defaultCountsEnabled);
        }
    }
    enabled = PatternMatchUtils.smartMatch(name, this.enabledStatsPatterns);
    if (enabled != null) {
        bean.setStatsEnabled(enabled);
        metrics.setFullStatsEnabled(enabled);
    } else {
        if (!overrides.statsConfigured) {
            bean.setStatsEnabled(this.defaultStatsEnabled);
            metrics.setFullStatsEnabled(this.defaultStatsEnabled);
        }
    }
    if (bean instanceof ConfigurableMetricsAware && !overrides.metricsConfigured) {
        ((ConfigurableMetricsAware<AbstractMessageChannelMetrics>) bean).configureMetrics(metrics);
    }
    this.channelsByName.put(name, bean);
}
Also used : ConfigurableMetricsAware(org.springframework.integration.support.management.ConfigurableMetricsAware) AbstractMessageChannelMetrics(org.springframework.integration.support.management.AbstractMessageChannelMetrics) PollableChannelManagement(org.springframework.integration.support.management.PollableChannelManagement) ManagementOverrides(org.springframework.integration.support.management.IntegrationManagement.ManagementOverrides)

Example 2 with AbstractMessageChannelMetrics

use of org.springframework.integration.support.management.AbstractMessageChannelMetrics in project spring-integration by spring-projects.

the class AbstractMessageChannel method send.

/**
 * Send a message on this channel. If the channel is at capacity, this
 * method will block until either the timeout occurs or the sending thread
 * is interrupted. If the specified timeout is 0, the method will return
 * immediately. If less than zero, it will block indefinitely (see
 * {@link #send(Message)}).
 * @param message the Message to send
 * @param timeout the timeout in milliseconds
 * @return <code>true</code> if the message is sent successfully,
 * <code>false</code> if the message cannot be sent within the allotted
 * time or the sending thread is interrupted.
 */
@Override
public boolean send(Message<?> message, long timeout) {
    Assert.notNull(message, "message must not be null");
    Assert.notNull(message.getPayload(), "message payload must not be null");
    if (this.shouldTrack) {
        message = MessageHistory.write(message, this, this.getMessageBuilderFactory());
    }
    Deque<ChannelInterceptor> interceptorStack = null;
    boolean sent = false;
    boolean metricsProcessed = false;
    MetricsContext metrics = null;
    boolean countsEnabled = this.countsEnabled;
    ChannelInterceptorList interceptors = this.interceptors;
    AbstractMessageChannelMetrics channelMetrics = this.channelMetrics;
    SampleFacade sample = null;
    try {
        if (this.datatypes.length > 0) {
            message = this.convertPayloadIfNecessary(message);
        }
        boolean debugEnabled = this.loggingEnabled && logger.isDebugEnabled();
        if (debugEnabled) {
            logger.debug("preSend on channel '" + this + "', message: " + message);
        }
        if (interceptors.getSize() > 0) {
            interceptorStack = new ArrayDeque<>();
            message = interceptors.preSend(message, this, interceptorStack);
            if (message == null) {
                return false;
            }
        }
        if (countsEnabled) {
            metrics = channelMetrics.beforeSend();
            if (this.metricsCaptor != null) {
                sample = this.metricsCaptor.start();
            }
            sent = doSend(message, timeout);
            if (sample != null) {
                sample.stop(sendTimer(sent));
            }
            channelMetrics.afterSend(metrics, sent);
            metricsProcessed = true;
        } else {
            sent = doSend(message, timeout);
        }
        if (debugEnabled) {
            logger.debug("postSend (sent=" + sent + ") on channel '" + this + "', message: " + message);
        }
        if (interceptorStack != null) {
            interceptors.postSend(message, this, sent);
            interceptors.afterSendCompletion(message, this, sent, null, interceptorStack);
        }
        return sent;
    } catch (Exception e) {
        if (countsEnabled && !metricsProcessed) {
            if (sample != null) {
                sample.stop(buildSendTimer(false, e.getClass().getSimpleName()));
            }
            channelMetrics.afterSend(metrics, false);
        }
        if (interceptorStack != null) {
            interceptors.afterSendCompletion(message, this, sent, e, interceptorStack);
        }
        throw IntegrationUtils.wrapInDeliveryExceptionIfNecessary(message, () -> "failed to send Message to channel '" + this.getComponentName() + "'", e);
    }
}
Also used : SampleFacade(org.springframework.integration.support.management.metrics.SampleFacade) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) MetricsContext(org.springframework.integration.support.management.MetricsContext) AbstractMessageChannelMetrics(org.springframework.integration.support.management.AbstractMessageChannelMetrics) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException)

Aggregations

AbstractMessageChannelMetrics (org.springframework.integration.support.management.AbstractMessageChannelMetrics)2 ConfigurableMetricsAware (org.springframework.integration.support.management.ConfigurableMetricsAware)1 ManagementOverrides (org.springframework.integration.support.management.IntegrationManagement.ManagementOverrides)1 MetricsContext (org.springframework.integration.support.management.MetricsContext)1 PollableChannelManagement (org.springframework.integration.support.management.PollableChannelManagement)1 SampleFacade (org.springframework.integration.support.management.metrics.SampleFacade)1 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)1 ChannelInterceptor (org.springframework.messaging.support.ChannelInterceptor)1