001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.messaging.simp.config;
018
019import org.springframework.messaging.MessageChannel;
020import org.springframework.messaging.SubscribableChannel;
021import org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler;
022import org.springframework.scheduling.TaskScheduler;
023
024/**
025 * Registration class for configuring a {@link SimpleBrokerMessageHandler}.
026 *
027 * @author Rossen Stoyanchev
028 * @since 4.0
029 */
030public class SimpleBrokerRegistration extends AbstractBrokerRegistration {
031
032        private TaskScheduler taskScheduler;
033
034        private long[] heartbeat;
035
036        private String selectorHeaderName = "selector";
037
038
039        public SimpleBrokerRegistration(SubscribableChannel inChannel, MessageChannel outChannel, String[] prefixes) {
040                super(inChannel, outChannel, prefixes);
041        }
042
043
044        /**
045         * Configure the {@link org.springframework.scheduling.TaskScheduler} to
046         * use for providing heartbeat support. Setting this property also sets the
047         * {@link #setHeartbeatValue heartbeatValue} to "10000, 10000".
048         * <p>By default this is not set.
049         * @since 4.2
050         */
051        public SimpleBrokerRegistration setTaskScheduler(TaskScheduler taskScheduler) {
052                this.taskScheduler = taskScheduler;
053                return this;
054        }
055
056        /**
057         * Configure the value for the heartbeat settings. The first number
058         * represents how often the server will write or send a heartbeat.
059         * The second is how often the client should write. 0 means no heartbeats.
060         * <p>By default this is set to "0, 0" unless the {@link #setTaskScheduler
061         * taskScheduler} in which case the default becomes "10000,10000"
062         * (in milliseconds).
063         * @since 4.2
064         */
065        public SimpleBrokerRegistration setHeartbeatValue(long[] heartbeat) {
066                this.heartbeat = heartbeat;
067                return this;
068        }
069
070        /**
071         * Configure the name of a header that a subscription message can have for
072         * the purpose of filtering messages matched to the subscription. The header
073         * value is expected to be a Spring EL boolean expression to be applied to
074         * the headers of messages matched to the subscription.
075         * <p>For example:
076         * <pre>
077         * headers.foo == 'bar'
078         * </pre>
079         * <p>By default this is set to "selector". You can set it to a different
080         * name, or to {@code null} to turn off support for a selector header.
081         * @param selectorHeaderName the name to use for a selector header
082         * @since 4.3.17
083         */
084        public void setSelectorHeaderName(String selectorHeaderName) {
085                this.selectorHeaderName = selectorHeaderName;
086        }
087
088
089        @Override
090        protected SimpleBrokerMessageHandler getMessageHandler(SubscribableChannel brokerChannel) {
091                SimpleBrokerMessageHandler handler = new SimpleBrokerMessageHandler(getClientInboundChannel(),
092                                getClientOutboundChannel(), brokerChannel, getDestinationPrefixes());
093                if (this.taskScheduler != null) {
094                        handler.setTaskScheduler(this.taskScheduler);
095                }
096                if (this.heartbeat != null) {
097                        handler.setHeartbeatValue(this.heartbeat);
098                }
099                handler.setSelectorHeaderName(this.selectorHeaderName);
100                return handler;
101        }
102
103}