
001/*- 002 * #%L 003 * HAPI FHIR - Server Framework 004 * %% 005 * Copyright (C) 2014 - 2025 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.rest.server.messaging; 021 022import ca.uhn.fhir.rest.server.messaging.json.BaseJsonMessage; 023import jakarta.annotation.Nonnull; 024 025import java.util.Map; 026import java.util.Optional; 027import java.util.UUID; 028 029/** 030 * This interface is implemented by serializable "wrapper" message classes that are exchanged with Massage Brokers. HAPI-FHIR 031 * message classes implement both {@link org.springframework.messaging.Message} and {@link IMessage} so that they can 032 * be exchanged with both JMS and non-JMS brokers. These message wrappers wrap a serializable payload that is the main content 033 * of the message. This wrapper also contains meta-data about the message such as headers and a message key. The message key 034 * is used by non-JMS brokers for partition selection. 035 * 036 * @param <T> the type of the message payload. In most cases, T will be a subclass of {@link BaseJsonMessage} 037 */ 038public interface IMessage<T> { 039 /** 040 * The message key is used by brokers that support channel partitioning. The message key is used to determine which partition 041 * a message is stored on. If message order is important, then the same message key should be used for all messages that need 042 * to preserve their order. E.g. if a series of messages create, update, and delete a resource, the resource id would be a good 043 * candidate for the message key to ensure the order of operations is preserved on all messages concerning that resource. 044 * @return the key of the message. 045 */ 046 @Nonnull 047 default String getMessageKey() { 048 return UUID.randomUUID().toString(); 049 } 050 051 /** 052 * @return a map of message headers 053 */ 054 @Nonnull 055 Map<String, Object> getHeaders(); 056 057 /** 058 * @return a header value as an Optional 059 */ 060 default <H> Optional<H> getHeader(String theHeaderName) { 061 return (Optional<H>) Optional.ofNullable(getHeaders().get(theHeaderName)); 062 } 063 064 /** 065 * @return the de-serialized value of the message 066 */ 067 T getPayload(); 068}