Reflect grouping in directory structure
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2022 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
|
||||
*
|
||||
* https://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 com.example.websocket.stomp;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.awaitility.Awaitility;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.aot.smoketest.support.assertj.AssertableOutput;
|
||||
import org.springframework.aot.smoketest.support.junit.ApplicationTest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@ApplicationTest
|
||||
class WebSocketStompApplicationAotTests {
|
||||
|
||||
@Test
|
||||
void clientShouldConnect(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10))
|
||||
.untilAsserted(() -> assertThat(output).hasSingleLineContaining("STOMP Client connected"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void serverShouldReceiveMessage(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> assertThat(output)
|
||||
.hasSingleLineContaining("Server: Received 'HelloMessage{name='STOMP Client'}'"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void clientShouldReceiveReply(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> assertThat(output)
|
||||
.hasSingleLineContaining("Client: Received 'GreetingMessage{content='Hello, STOMP Client!'}'"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.example.websocket.stomp;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.example.websocket.stomp.dto.GreetingMessage;
|
||||
import com.example.websocket.stomp.dto.HelloMessage;
|
||||
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
|
||||
import org.springframework.messaging.simp.stomp.StompHeaders;
|
||||
import org.springframework.messaging.simp.stomp.StompSession;
|
||||
import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.socket.messaging.WebSocketStompClient;
|
||||
|
||||
@Component
|
||||
class CLR implements CommandLineRunner {
|
||||
|
||||
private final WebSocketStompClient webSocketStompClient;
|
||||
|
||||
private final ServletWebServerApplicationContext servletWebServerApplicationContext;
|
||||
|
||||
CLR(WebSocketStompClient webSocketStompClient,
|
||||
ServletWebServerApplicationContext servletWebServerApplicationContext) {
|
||||
this.webSocketStompClient = webSocketStompClient;
|
||||
this.servletWebServerApplicationContext = servletWebServerApplicationContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
MyStompHandler handler = new MyStompHandler();
|
||||
StompSession stompSession = this.webSocketStompClient
|
||||
.connect("ws://localhost:%d/stomp".formatted(getServerPort()), handler).get(5, TimeUnit.SECONDS);
|
||||
|
||||
stompSession.subscribe("/topic/greetings", handler);
|
||||
stompSession.send("/app/hello", new HelloMessage("STOMP Client"));
|
||||
}
|
||||
|
||||
private int getServerPort() {
|
||||
return this.servletWebServerApplicationContext.getWebServer().getPort();
|
||||
}
|
||||
|
||||
private static class MyStompHandler extends StompSessionHandlerAdapter {
|
||||
|
||||
@Override
|
||||
public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
|
||||
System.out.println("STOMP Client connected");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getPayloadType(StompHeaders headers) {
|
||||
return GreetingMessage.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleFrame(StompHeaders headers, Object payload) {
|
||||
System.out.printf("Client: Received '%s'%n", payload);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.example.websocket.stomp;
|
||||
|
||||
import com.example.websocket.stomp.dto.GreetingMessage;
|
||||
import com.example.websocket.stomp.dto.HelloMessage;
|
||||
|
||||
import org.springframework.messaging.handler.annotation.MessageMapping;
|
||||
import org.springframework.messaging.handler.annotation.SendTo;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.util.HtmlUtils;
|
||||
|
||||
@Controller
|
||||
public class GreetingController {
|
||||
|
||||
@MessageMapping("/hello")
|
||||
@SendTo("/topic/greetings")
|
||||
public GreetingMessage greeting(HelloMessage message) throws Exception {
|
||||
System.out.printf("Server: Received '%s'%n", message);
|
||||
Thread.sleep(1000); // simulated delay
|
||||
return new GreetingMessage("Hello, " + HtmlUtils.htmlEscape(message.getName()) + "!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.example.websocket.stomp;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.converter.MessageConverter;
|
||||
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.web.socket.client.WebSocketClient;
|
||||
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
|
||||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
|
||||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
|
||||
import org.springframework.web.socket.messaging.WebSocketStompClient;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSocketMessageBroker
|
||||
class WebSocketBrokerConfig implements WebSocketMessageBrokerConfigurer {
|
||||
|
||||
@Override
|
||||
public void configureMessageBroker(MessageBrokerRegistry config) {
|
||||
config.enableSimpleBroker("/topic");
|
||||
config.setApplicationDestinationPrefixes("/app");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerStompEndpoints(StompEndpointRegistry registry) {
|
||||
registry.addEndpoint("/stomp");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public WebSocketStompClient webSocketStompClient(WebSocketClient webSocketClient, TaskScheduler taskScheduler,
|
||||
MessageConverter messageConverter) {
|
||||
WebSocketStompClient client = new WebSocketStompClient(webSocketClient);
|
||||
client.setMessageConverter(messageConverter);
|
||||
client.setTaskScheduler(taskScheduler);
|
||||
return client;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public WebSocketClient webSocketClient() {
|
||||
return new StandardWebSocketClient();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.websocket.stomp;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class WebSocketStompApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(WebSocketStompApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.example.websocket.stomp.dto;
|
||||
|
||||
public class GreetingMessage {
|
||||
|
||||
private String content;
|
||||
|
||||
public GreetingMessage() {
|
||||
}
|
||||
|
||||
public GreetingMessage(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "GreetingMessage{" + "content='" + content + '\'' + '}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.example.websocket.stomp.dto;
|
||||
|
||||
public class HelloMessage {
|
||||
|
||||
private String name;
|
||||
|
||||
public HelloMessage() {
|
||||
}
|
||||
|
||||
public HelloMessage(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "HelloMessage{" + "name='" + name + '\'' + '}';
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user