001/* 002 * Copyright 2002-2017 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.support; 018 019import org.springframework.lang.Nullable; 020import org.springframework.messaging.MessageHeaders; 021import org.springframework.util.IdGenerator; 022 023/** 024 * A {@link org.springframework.messaging.support.MessageHeaderInitializer MessageHeaderInitializer} 025 * to customize the strategy for ID and TIMESTAMP message header generation. 026 * 027 * @author Rossen Stoyanchev 028 * @since 4.1 029 */ 030public class IdTimestampMessageHeaderInitializer implements MessageHeaderInitializer { 031 032 private static final IdGenerator ID_VALUE_NONE_GENERATOR = () -> MessageHeaders.ID_VALUE_NONE; 033 034 035 @Nullable 036 private IdGenerator idGenerator; 037 038 private boolean enableTimestamp; 039 040 041 /** 042 * Configure the IdGenerator strategy to initialize {@code MessageHeaderAccessor} 043 * instances with. 044 * <p>By default this property is set to {@code null} in which case the default 045 * IdGenerator of {@link org.springframework.messaging.MessageHeaders} is used. 046 * <p>To have no ids generated at all, see {@link #setDisableIdGeneration()}. 047 */ 048 public void setIdGenerator(@Nullable IdGenerator idGenerator) { 049 this.idGenerator = idGenerator; 050 } 051 052 /** 053 * Return the configured {@code IdGenerator}, if any. 054 */ 055 @Nullable 056 public IdGenerator getIdGenerator() { 057 return this.idGenerator; 058 } 059 060 /** 061 * A shortcut for calling {@link #setIdGenerator} with an id generation strategy 062 * to disable id generation completely. 063 */ 064 public void setDisableIdGeneration() { 065 this.idGenerator = ID_VALUE_NONE_GENERATOR; 066 } 067 068 /** 069 * Whether to enable the automatic addition of the 070 * {@link org.springframework.messaging.MessageHeaders#TIMESTAMP} header on 071 * {@code MessageHeaderAccessor} instances being initialized. 072 * <p>By default this property is set to false. 073 */ 074 public void setEnableTimestamp(boolean enableTimestamp) { 075 this.enableTimestamp = enableTimestamp; 076 } 077 078 /** 079 * Return whether the timestamp header is enabled or not. 080 */ 081 public boolean isEnableTimestamp() { 082 return this.enableTimestamp; 083 } 084 085 086 @Override 087 public void initHeaders(MessageHeaderAccessor headerAccessor) { 088 IdGenerator idGenerator = getIdGenerator(); 089 if (idGenerator != null) { 090 headerAccessor.setIdGenerator(idGenerator); 091 } 092 headerAccessor.setEnableTimestamp(isEnableTimestamp()); 093 } 094 095}