Browse Source
MessageHolder holds the currently processed message in a ThreadLocal, which allows PubSubMessageBuilder to automatically add a session id to messages to be sent.pull/286/merge
7 changed files with 150 additions and 77 deletions
@ -0,0 +1,45 @@
@@ -0,0 +1,45 @@
|
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.messaging.support; |
||||
|
||||
import org.springframework.core.NamedThreadLocal; |
||||
import org.springframework.messaging.Message; |
||||
|
||||
|
||||
/** |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.0 |
||||
*/ |
||||
public class MessageHolder { |
||||
|
||||
private static final NamedThreadLocal<Message<?>> messageHolder = |
||||
new NamedThreadLocal<Message<?>>("Current message"); |
||||
|
||||
|
||||
public static void setMessage(Message<?> message) { |
||||
messageHolder.set(message); |
||||
} |
||||
|
||||
public static Message<?> getMessage() { |
||||
return messageHolder.get(); |
||||
} |
||||
|
||||
public static void reset() { |
||||
messageHolder.remove(); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,77 @@
@@ -0,0 +1,77 @@
|
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.messaging.support; |
||||
|
||||
import org.springframework.http.MediaType; |
||||
import org.springframework.messaging.Message; |
||||
import org.springframework.messaging.support.MessageBuilder; |
||||
|
||||
import reactor.util.Assert; |
||||
|
||||
|
||||
/** |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.0 |
||||
*/ |
||||
public class PubSubMessageBuilder<T> { |
||||
|
||||
private final PubSubHeaderAccesssor headers = PubSubHeaderAccesssor.create(); |
||||
|
||||
private final T payload; |
||||
|
||||
|
||||
private PubSubMessageBuilder(T payload) { |
||||
Assert.notNull(payload, "<T> is required"); |
||||
this.payload = payload; |
||||
} |
||||
|
||||
|
||||
public static <T> PubSubMessageBuilder<T> withPayload(T payload) { |
||||
return new PubSubMessageBuilder<T>(payload); |
||||
} |
||||
|
||||
|
||||
public PubSubMessageBuilder<T> destination(String destination) { |
||||
Assert.notNull(destination, "destination is required"); |
||||
this.headers.setDestination(destination); |
||||
return this; |
||||
} |
||||
|
||||
public PubSubMessageBuilder<T> contentType(MediaType contentType) { |
||||
Assert.notNull(contentType, "contentType is required"); |
||||
this.headers.setContentType(contentType); |
||||
return this; |
||||
} |
||||
|
||||
public PubSubMessageBuilder<T> contentType(String contentType) { |
||||
Assert.notNull(contentType, "contentType is required"); |
||||
this.headers.setContentType(MediaType.parseMediaType(contentType)); |
||||
return this; |
||||
} |
||||
|
||||
public Message<T> build() { |
||||
|
||||
Message<?> message = MessageHolder.getMessage(); |
||||
if (message != null) { |
||||
String sessionId = PubSubHeaderAccesssor.wrap(message).getSessionId(); |
||||
this.headers.setSessionId(sessionId); |
||||
} |
||||
|
||||
return MessageBuilder.withPayload(this.payload).copyHeaders(this.headers.toHeaders()).build(); |
||||
} |
||||
|
||||
} |
||||
@ -1,60 +0,0 @@
@@ -1,60 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2013 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.messaging.support; |
||||
|
||||
import org.springframework.messaging.Message; |
||||
import org.springframework.messaging.MessageChannel; |
||||
import org.springframework.messaging.support.MessageBuilder; |
||||
|
||||
import reactor.util.Assert; |
||||
|
||||
|
||||
/** |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.0 |
||||
*/ |
||||
@SuppressWarnings("rawtypes") |
||||
public class SessionMessageChannel<M extends Message> implements MessageChannel<M> { |
||||
|
||||
private MessageChannel<M> delegate; |
||||
|
||||
private final String sessionId; |
||||
|
||||
|
||||
public SessionMessageChannel(MessageChannel<M> delegate, String sessionId) { |
||||
Assert.notNull(delegate, "delegate is required"); |
||||
Assert.notNull(sessionId, "sessionId is required"); |
||||
this.sessionId = sessionId; |
||||
this.delegate = delegate; |
||||
} |
||||
|
||||
@Override |
||||
public boolean send(M message) { |
||||
return send(message, -1); |
||||
} |
||||
|
||||
@Override |
||||
public boolean send(M message, long timeout) { |
||||
PubSubHeaderAccesssor headers = PubSubHeaderAccesssor.wrap(message); |
||||
headers.setSessionId(this.sessionId); |
||||
Object payload = message.getPayload(); |
||||
@SuppressWarnings("unchecked") |
||||
M messageToSend = (M) MessageBuilder.withPayload(payload).copyHeaders(headers.toHeaders()).build(); |
||||
this.delegate.send(messageToSend); |
||||
return true; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue