Example usage for org.springframework.util Assert state

List of usage examples for org.springframework.util Assert state

Introduction

In this page you can find the example usage for org.springframework.util Assert state.

Prototype

public static void state(boolean expression, Supplier<String> messageSupplier) 

Source Link

Document

Assert a boolean expression, throwing an IllegalStateException if the expression evaluates to false .

Usage

From source file:org.springframework.amqp.rabbit.annotation.RabbitListenerAnnotationBeanPostProcessor.java

@Override
public void afterSingletonsInstantiated() {
    this.registrar.setBeanFactory(this.beanFactory);

    if (this.beanFactory instanceof ListableBeanFactory) {
        Map<String, RabbitListenerConfigurer> instances = ((ListableBeanFactory) this.beanFactory)
                .getBeansOfType(RabbitListenerConfigurer.class);
        for (RabbitListenerConfigurer configurer : instances.values()) {
            configurer.configureRabbitListeners(this.registrar);
        }/*from   w w w. ja  v a2s .co m*/
    }

    if (this.registrar.getEndpointRegistry() == null) {
        if (this.endpointRegistry == null) {
            Assert.state(this.beanFactory != null,
                    "BeanFactory must be set to find endpoint registry by bean name");
            this.endpointRegistry = this.beanFactory.getBean(
                    RabbitListenerConfigUtils.RABBIT_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME,
                    RabbitListenerEndpointRegistry.class);
        }
        this.registrar.setEndpointRegistry(this.endpointRegistry);
    }

    if (this.containerFactoryBeanName != null) {
        this.registrar.setContainerFactoryBeanName(this.containerFactoryBeanName);
    }

    // Set the custom handler method factory once resolved by the configurer
    MessageHandlerMethodFactory handlerMethodFactory = this.registrar.getMessageHandlerMethodFactory();
    if (handlerMethodFactory != null) {
        this.messageHandlerMethodFactory.setMessageHandlerMethodFactory(handlerMethodFactory);
    }

    // Actually register all listeners
    this.registrar.afterPropertiesSet();
}

From source file:org.springframework.amqp.rabbit.annotation.RabbitListenerAnnotationBeanPostProcessor.java

protected void processListener(MethodRabbitListenerEndpoint endpoint, RabbitListener rabbitListener,
        Object bean, Object adminTarget, String beanName) {
    endpoint.setBean(bean);/*from   www .ja v  a  2s  .c o  m*/
    endpoint.setMessageHandlerMethodFactory(this.messageHandlerMethodFactory);
    endpoint.setId(getEndpointId(rabbitListener));
    endpoint.setQueueNames(resolveQueues(rabbitListener));
    String group = rabbitListener.group();
    if (StringUtils.hasText(group)) {
        Object resolvedGroup = resolveExpression(group);
        if (resolvedGroup instanceof String) {
            endpoint.setGroup((String) resolvedGroup);
        }
    }

    endpoint.setExclusive(rabbitListener.exclusive());
    String priority = resolve(rabbitListener.priority());
    if (StringUtils.hasText(priority)) {
        try {
            endpoint.setPriority(Integer.valueOf(priority));
        } catch (NumberFormatException ex) {
            throw new BeanInitializationException(
                    "Invalid priority value for " + rabbitListener + " (must be an integer)", ex);
        }
    }

    String rabbitAdmin = resolve(rabbitListener.admin());
    if (StringUtils.hasText(rabbitAdmin)) {
        Assert.state(this.beanFactory != null, "BeanFactory must be set to resolve RabbitAdmin by bean name");
        try {
            endpoint.setAdmin(this.beanFactory.getBean(rabbitAdmin, RabbitAdmin.class));
        } catch (NoSuchBeanDefinitionException ex) {
            throw new BeanInitializationException("Could not register rabbit listener endpoint on ["
                    + adminTarget + "], no " + RabbitAdmin.class.getSimpleName() + " with id '" + rabbitAdmin
                    + "' was found in the application context", ex);
        }
    }

    RabbitListenerContainerFactory<?> factory = null;
    String containerFactoryBeanName = resolve(rabbitListener.containerFactory());
    if (StringUtils.hasText(containerFactoryBeanName)) {
        Assert.state(this.beanFactory != null,
                "BeanFactory must be set to obtain container factory by bean name");
        try {
            factory = this.beanFactory.getBean(containerFactoryBeanName, RabbitListenerContainerFactory.class);
        } catch (NoSuchBeanDefinitionException ex) {
            throw new BeanInitializationException(
                    "Could not register rabbit listener endpoint on [" + adminTarget + "] for bean " + beanName
                            + ", no " + RabbitListenerContainerFactory.class.getSimpleName() + " with id '"
                            + containerFactoryBeanName + "' was found in the application context",
                    ex);
        }
    }

    this.registrar.registerEndpoint(endpoint, factory);
}

From source file:org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.java

protected String[] getRequiredQueueNames() {
    Assert.state(this.queueNames.size() > 0, "Queue names must not be empty.");
    return this.getQueueNames();
}

From source file:org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.java

/**
 * Delegates to {@link #validateConfiguration()} and {@link #initialize()}.
 *//*from ww w  .j  ava 2 s  . c  o m*/
@Override
public final void afterPropertiesSet() {
    super.afterPropertiesSet();
    Assert.state(this.exposeListenerChannel || !getAcknowledgeMode().isManual(),
            "You cannot acknowledge messages manually if the channel is not exposed to the listener "
                    + "(please check your configuration and set exposeListenerChannel=true or acknowledgeMode!=MANUAL)");
    Assert.state(!(getAcknowledgeMode().isAutoAck() && isChannelTransacted()),
            "The acknowledgeMode is NONE (autoack in Rabbit terms) which is not consistent with having a "
                    + "transactional channel. Either use a different AcknowledgeMode or make sure channelTransacted=false");
    validateConfiguration();
    initialize();
}

From source file:org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener.java

private Address evaluateReplyTo(Message request, Object source, Object result, Expression expression) {
    Address replyTo = null;// w  ww  .ja  v a2 s  .  c o m
    Object value = expression.getValue(this.evalContext, new ReplyExpressionRoot(request, source, result));
    Assert.state(value instanceof String || value instanceof Address,
            "response expression must evaluate to a String or Address");
    if (value instanceof String) {
        replyTo = new Address((String) value);
    } else {
        replyTo = (Address) value;
    }
    return replyTo;
}

From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer.java

@Override
public void setQueueNames(String... queueName) {
    Assert.state(!isRunning(), "Cannot set queue names while running, use add/remove");
    super.setQueueNames(queueName);
}

From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer.java

@Override
public void setQueues(Queue... queues) {
    Assert.state(!isRunning(), "Cannot set queue names while running, use add/remove");
    super.setQueues(queues);
}

From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer.java

private void checkStartState() {
    if (!this.isRunning()) {
        try {// ww w . j av  a  2  s  .co m
            Assert.state(this.startedLatch.await(60, TimeUnit.SECONDS),
                    "Container is not started - cannot adjust queues");
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new AmqpException("Interrupted waiting for start", e);
        }
    }
}

From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer.java

/**
 * Must hold this.consumersMonitor// ww  w .  j ava  2s. c o m
 */
private void actualShutDown() {
    Assert.state(getTaskExecutor() != null, "Cannot shut down if not initialized");
    logger.debug("Shutting down");
    // Copy in the same order to avoid ConcurrentModificationException during remove in the cancelConsumer().
    new LinkedList<>(this.consumers).forEach(this::cancelConsumer);
    this.consumers.clear();
    this.consumersByQueue.clear();
    logger.debug("All consumers canceled");
    if (this.consumerMonitorTask != null) {
        this.consumerMonitorTask.cancel(true);
        this.consumerMonitorTask = null;
    }
}

From source file:org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry.java

/**
 * Create a message listener container for the given {@link RabbitListenerEndpoint}.
 * <p>This create the necessary infrastructure to honor that endpoint
 * with regards to its configuration.//from   ww w.  jav  a2  s  . com
 * <p>The {@code startImmediately} flag determines if the container should be
 * started immediately.
 * @param endpoint the endpoint to add.
 * @param factory the {@link RabbitListenerContainerFactory} to use.
 * @param startImmediately start the container immediately if necessary
 * @see #getListenerContainers()
 * @see #getListenerContainer(String)
 */
@SuppressWarnings("unchecked")
public void registerListenerContainer(RabbitListenerEndpoint endpoint,
        RabbitListenerContainerFactory<?> factory, boolean startImmediately) {
    Assert.notNull(endpoint, "Endpoint must not be null");
    Assert.notNull(factory, "Factory must not be null");

    String id = endpoint.getId();
    Assert.hasText(id, "Endpoint id must not be empty");
    synchronized (this.listenerContainers) {
        Assert.state(!this.listenerContainers.containsKey(id),
                "Another endpoint is already registered with id '" + id + "'");
        MessageListenerContainer container = createListenerContainer(endpoint, factory);
        this.listenerContainers.put(id, container);
        if (StringUtils.hasText(endpoint.getGroup()) && this.applicationContext != null) {
            List<MessageListenerContainer> containerGroup;
            if (this.applicationContext.containsBean(endpoint.getGroup())) {
                containerGroup = this.applicationContext.getBean(endpoint.getGroup(), List.class);
            } else {
                containerGroup = new ArrayList<MessageListenerContainer>();
                this.applicationContext.getBeanFactory().registerSingleton(endpoint.getGroup(), containerGroup);
            }
            containerGroup.add(container);
        }
        if (startImmediately) {
            startIfNecessary(container);
        }
    }
}