forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Java config support for WebSocket and STOMP
Issue: SPR-10835
- Loading branch information
1 parent
4b6a9ac
commit 4c0da58
Showing
31 changed files
with
2,236 additions
and
290 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...g/src/main/java/org/springframework/messaging/simp/config/AbstractBrokerRegistration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* 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.messaging.simp.config; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
import org.springframework.messaging.MessageChannel; | ||
import org.springframework.messaging.simp.handler.AbstractBrokerMessageHandler; | ||
|
||
import reactor.util.Assert; | ||
|
||
|
||
/** | ||
* Base class for message broker registration classes. | ||
* | ||
* @author Rossen Stoyanchev | ||
* @since 4.0 | ||
*/ | ||
public abstract class AbstractBrokerRegistration { | ||
|
||
private final MessageChannel webSocketReplyChannel; | ||
|
||
private final String[] destinationPrefixes; | ||
|
||
|
||
public AbstractBrokerRegistration(MessageChannel webSocketReplyChannel, String[] destinationPrefixes) { | ||
Assert.notNull(webSocketReplyChannel, ""); | ||
this.webSocketReplyChannel = webSocketReplyChannel; | ||
this.destinationPrefixes = destinationPrefixes; | ||
} | ||
|
||
|
||
protected MessageChannel getWebSocketReplyChannel() { | ||
return this.webSocketReplyChannel; | ||
} | ||
|
||
protected Collection<String> getDestinationPrefixes() { | ||
return (this.destinationPrefixes != null) | ||
? Arrays.<String>asList(this.destinationPrefixes) : Collections.<String>emptyList(); | ||
} | ||
|
||
protected abstract AbstractBrokerMessageHandler getMessageHandler(); | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
.../springframework/messaging/simp/config/DelegatingWebSocketMessageBrokerConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* 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.messaging.simp.config; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
|
||
/** | ||
* A {@link WebSocketMessageBrokerConfiguration} extension that detects beans of type | ||
* {@link WebSocketMessageBrokerConfigurer} and delegates to all of them allowing callback | ||
* style customization of the configuration provided in | ||
* {@link WebSocketMessageBrokerConfigurationSupport}. | ||
* | ||
* <p>This class is typically imported via {@link EnableWebSocketMessageBroker}. | ||
* | ||
* @author Rossen Stoyanchev | ||
* @since 4.0 | ||
*/ | ||
@Configuration | ||
public class DelegatingWebSocketMessageBrokerConfiguration extends WebSocketMessageBrokerConfigurationSupport { | ||
|
||
private List<WebSocketMessageBrokerConfigurer> configurers = new ArrayList<WebSocketMessageBrokerConfigurer>(); | ||
|
||
|
||
@Autowired(required=false) | ||
public void setConfigurers(List<WebSocketMessageBrokerConfigurer> configurers) { | ||
if (CollectionUtils.isEmpty(configurers)) { | ||
return; | ||
} | ||
this.configurers.addAll(configurers); | ||
} | ||
|
||
@Override | ||
protected void registerStompEndpoints(StompEndpointRegistry registry) { | ||
for (WebSocketMessageBrokerConfigurer c : this.configurers) { | ||
c.registerStompEndpoints(registry); | ||
} | ||
} | ||
|
||
@Override | ||
protected void configureMessageBroker(MessageBrokerConfigurer configurer) { | ||
for (WebSocketMessageBrokerConfigurer c : this.configurers) { | ||
c.configureMessageBroker(configurer); | ||
} | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
...src/main/java/org/springframework/messaging/simp/config/EnableWebSocketMessageBroker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* 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.messaging.simp.config; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.springframework.context.annotation.Import; | ||
|
||
|
||
/** | ||
* Add this annotation to an {@code @Configuration} class to enable broker-backed | ||
* messaging over WebSocket using a higher-level messaging sub-protocol. | ||
* | ||
* <pre class="code"> | ||
* @Configuration | ||
* @EnableWebSocketMessageBroker | ||
* public class MyWebSocketConfig { | ||
* | ||
* } | ||
* </pre> | ||
* <p> | ||
* Customize the imported configuration by implementing the | ||
* {@link WebSocketMessageBrokerConfigurer} interface: | ||
* | ||
* <pre class="code"> | ||
* @Configuration | ||
* @EnableWebSocketMessageBroker | ||
* public class MyConfiguration implements implements WebSocketMessageBrokerConfigurer { | ||
* | ||
* @Override | ||
* public void registerStompEndpoints(StompEndpointRegistry registry) { | ||
* registry.addEndpoint("/portfolio").withSockJS(); | ||
* } | ||
* | ||
* @Bean | ||
* public void configureMessageBroker(MessageBrokerConfigurer configurer) { | ||
* configurer.enableStompBrokerRelay("/queue/", "/topic/"); | ||
* configurer.setAnnotationMethodDestinationPrefixes("/app/"); | ||
* } | ||
* } | ||
* </pre> | ||
* | ||
* @author Rossen Stoyanchev | ||
* @since 4.0 | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
@Documented | ||
@Import(DelegatingWebSocketMessageBrokerConfiguration.class) | ||
public @interface EnableWebSocketMessageBroker { | ||
} |
84 changes: 84 additions & 0 deletions
84
...ging/src/main/java/org/springframework/messaging/simp/config/MessageBrokerConfigurer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* 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.messaging.simp.config; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
import org.springframework.messaging.MessageChannel; | ||
import org.springframework.messaging.simp.handler.AbstractBrokerMessageHandler; | ||
|
||
import reactor.util.Assert; | ||
|
||
|
||
/** | ||
* A helper class for configuring message broker options. | ||
* | ||
* @author Rossen Stoyanchev | ||
* @since 4.0 | ||
*/ | ||
public class MessageBrokerConfigurer { | ||
|
||
private final MessageChannel webSocketReplyChannel; | ||
|
||
private SimpleBrokerRegistration simpleBroker; | ||
|
||
private StompBrokerRelayRegistration stompRelay; | ||
|
||
private String[] annotationMethodDestinationPrefixes; | ||
|
||
|
||
public MessageBrokerConfigurer(MessageChannel webSocketReplyChannel) { | ||
Assert.notNull(webSocketReplyChannel); | ||
this.webSocketReplyChannel = webSocketReplyChannel; | ||
} | ||
|
||
public SimpleBrokerRegistration enableSimpleBroker(String... destinationPrefixes) { | ||
this.simpleBroker = new SimpleBrokerRegistration(this.webSocketReplyChannel, destinationPrefixes); | ||
return this.simpleBroker; | ||
} | ||
|
||
public StompBrokerRelayRegistration enableStompBrokerRelay(String... destinationPrefixes) { | ||
this.stompRelay = new StompBrokerRelayRegistration(this.webSocketReplyChannel, destinationPrefixes); | ||
return this.stompRelay; | ||
} | ||
|
||
public MessageBrokerConfigurer setAnnotationMethodDestinationPrefixes(String... destinationPrefixes) { | ||
this.annotationMethodDestinationPrefixes = destinationPrefixes; | ||
return this; | ||
} | ||
|
||
protected AbstractBrokerMessageHandler getSimpleBroker() { | ||
initSimpleBrokerIfNecessary(); | ||
return (this.simpleBroker != null) ? this.simpleBroker.getMessageHandler() : null; | ||
} | ||
|
||
protected void initSimpleBrokerIfNecessary() { | ||
if ((this.simpleBroker == null) && (this.stompRelay == null)) { | ||
this.simpleBroker = new SimpleBrokerRegistration(this.webSocketReplyChannel, null); | ||
} | ||
} | ||
|
||
protected AbstractBrokerMessageHandler getStompBrokerRelay() { | ||
return (this.stompRelay != null) ? this.stompRelay.getMessageHandler() : null; | ||
} | ||
|
||
protected Collection<String> getAnnotationMethodDestinationPrefixes() { | ||
return (this.annotationMethodDestinationPrefixes != null) | ||
? Arrays.asList(this.annotationMethodDestinationPrefixes) : null; | ||
} | ||
} |
Oops, something went wrong.