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.lang.Nullable; 020import org.springframework.messaging.MessageChannel; 021import org.springframework.messaging.SubscribableChannel; 022import org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler; 023import org.springframework.scheduling.TaskScheduler; 024 025/** 026 * Registration class for configuring a {@link SimpleBrokerMessageHandler}. 027 * 028 * @author Rossen Stoyanchev 029 * @since 4.0 030 */ 031public class SimpleBrokerRegistration extends AbstractBrokerRegistration { 032 033 @Nullable 034 private TaskScheduler taskScheduler; 035 036 @Nullable 037 private long[] heartbeat; 038 039 @Nullable 040 private String selectorHeaderName = "selector"; 041 042 043 public SimpleBrokerRegistration(SubscribableChannel inChannel, MessageChannel outChannel, String[] prefixes) { 044 super(inChannel, outChannel, prefixes); 045 } 046 047 048 /** 049 * Configure the {@link org.springframework.scheduling.TaskScheduler} to 050 * use for providing heartbeat support. Setting this property also sets the 051 * {@link #setHeartbeatValue heartbeatValue} to "10000, 10000". 052 * <p>By default this is not set. 053 * @since 4.2 054 */ 055 public SimpleBrokerRegistration setTaskScheduler(TaskScheduler taskScheduler) { 056 this.taskScheduler = taskScheduler; 057 return this; 058 } 059 060 /** 061 * Configure the value for the heartbeat settings. The first number 062 * represents how often the server will write or send a heartbeat. 063 * The second is how often the client should write. 0 means no heartbeats. 064 * <p>By default this is set to "0, 0" unless the {@link #setTaskScheduler 065 * taskScheduler} in which case the default becomes "10000,10000" 066 * (in milliseconds). 067 * @since 4.2 068 */ 069 public SimpleBrokerRegistration setHeartbeatValue(long[] heartbeat) { 070 this.heartbeat = heartbeat; 071 return this; 072 } 073 074 /** 075 * Configure the name of a header that a subscription message can have for 076 * the purpose of filtering messages matched to the subscription. The header 077 * value is expected to be a Spring EL boolean expression to be applied to 078 * the headers of messages matched to the subscription. 079 * <p>For example: 080 * <pre> 081 * headers.foo == 'bar' 082 * </pre> 083 * <p>By default this is set to "selector". You can set it to a different 084 * name, or to {@code null} to turn off support for a selector header. 085 * @param selectorHeaderName the name to use for a selector header 086 * @since 4.3.17 087 */ 088 public void setSelectorHeaderName(@Nullable String selectorHeaderName) { 089 this.selectorHeaderName = selectorHeaderName; 090 } 091 092 093 @Override 094 protected SimpleBrokerMessageHandler getMessageHandler(SubscribableChannel brokerChannel) { 095 SimpleBrokerMessageHandler handler = new SimpleBrokerMessageHandler(getClientInboundChannel(), 096 getClientOutboundChannel(), brokerChannel, getDestinationPrefixes()); 097 if (this.taskScheduler != null) { 098 handler.setTaskScheduler(this.taskScheduler); 099 } 100 if (this.heartbeat != null) { 101 handler.setHeartbeatValue(this.heartbeat); 102 } 103 handler.setSelectorHeaderName(this.selectorHeaderName); 104 return handler; 105 } 106 107}