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