diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index 7477c78b58..1c34dfa7ca 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -125,19 +125,31 @@ mysql-connector-java 6.0.6 - + + org.springframework + spring-websocket + ${spring.version} + + + + org.springframework + spring-messaging + ${spring.version} + + + org.togglz togglz-spring-boot-starter ${togglz.version} - - + + org.togglz togglz-spring-security ${togglz.version} - + org.apache.activemq artemis-server diff --git a/spring-boot/src/main/java/org/baeldung/websocket/client/Message.java b/spring-boot/src/main/java/org/baeldung/websocket/client/Message.java new file mode 100644 index 0000000000..bcab44870b --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/websocket/client/Message.java @@ -0,0 +1,25 @@ +package org.baeldung.websocket.client; + +public class Message { + + private String from; + private String text; + + public String getText() { + return text; + } + + public String getFrom() { + return from; + } + + public void setFrom(String from) { + this.from = from; + } + + public void setText(String text) { + this.text = text; + } + + +} diff --git a/spring-boot/src/main/java/org/baeldung/websocket/client/MyStompSessionHandler.java b/spring-boot/src/main/java/org/baeldung/websocket/client/MyStompSessionHandler.java new file mode 100644 index 0000000000..35bf1827ea --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/websocket/client/MyStompSessionHandler.java @@ -0,0 +1,58 @@ +package org.baeldung.websocket.client; + +import org.apache.log4j.Logger; +import org.springframework.messaging.simp.stomp.StompCommand; +import org.springframework.messaging.simp.stomp.StompHeaders; +import org.springframework.messaging.simp.stomp.StompSession; +import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter; + +import java.lang.reflect.Type; + +/** + * This class is an implementation for StompSessionHandlerAdapter. + * Once a connection is established, We subscribe to /topic/messages and + * send a sample message to server. + * + * @author Kalyan + * + */ +public class MyStompSessionHandler extends StompSessionHandlerAdapter { + + private Logger logger = Logger.getLogger(MyStompSessionHandler.class); + + @Override + public void afterConnected(StompSession session, StompHeaders connectedHeaders) { + logger.info("New session established : "+session.getSessionId()); + session.subscribe("/topic/messages", this); + logger.info("Subscribed to /topic/messages"); + session.send("/app/chat", getSampleMessage()); + logger.info("Message sent to websocket server"); + } + + @Override + public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) { + logger.error("Got an exception", exception); + } + + @Override + public Type getPayloadType(StompHeaders headers) { + return Message.class; + } + + @Override + public void handleFrame(StompHeaders headers, Object payload) { + Message msg = (Message)payload; + logger.info("Received : "+ msg.getText()+ " from : "+msg.getFrom()); + } + + /** + * A sample message instance. + * @return instance of Message + */ + private Message getSampleMessage(){ + Message msg = new Message(); + msg.setFrom("Nicky"); + msg.setText("Howdy!!"); + return msg; + } +} \ No newline at end of file diff --git a/spring-boot/src/main/java/org/baeldung/websocket/client/StompClient.java b/spring-boot/src/main/java/org/baeldung/websocket/client/StompClient.java new file mode 100644 index 0000000000..da2dbeac3d --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/websocket/client/StompClient.java @@ -0,0 +1,37 @@ +package org.baeldung.websocket.client; + +import java.util.Collections; +import java.util.List; +import java.util.Scanner; + +import org.springframework.messaging.converter.MappingJackson2MessageConverter; +import org.springframework.messaging.simp.stomp.StompSessionHandler; +import org.springframework.web.socket.client.standard.StandardWebSocketClient; +import org.springframework.web.socket.messaging.WebSocketStompClient; +import org.springframework.web.socket.sockjs.client.SockJsClient; +import org.springframework.web.socket.sockjs.client.Transport; +import org.springframework.web.socket.sockjs.client.WebSocketTransport; + +/** + * Stand alone WebSocketStompClient. + * @author Kalyan + * + */ +public class StompClient { + + private static String URL = "ws://localhost:8080/spring-mvc-java/chat"; + + public static void main(String[] args) { + Transport webSocketTransport = new WebSocketTransport(new StandardWebSocketClient()); + List transports = Collections.singletonList(webSocketTransport); + SockJsClient sockJsClient = new SockJsClient(transports); + + WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient); + stompClient.setMessageConverter(new MappingJackson2MessageConverter()); + + StompSessionHandler sessionHandler = new MyStompSessionHandler(); + stompClient.connect(URL, sessionHandler); + + new Scanner(System.in).nextLine(); // Don't close immediately. + } +} diff --git a/spring-boot/src/test/java/com/baeldung/websocket/client/MyStompSessionHandlerTest.java b/spring-boot/src/test/java/com/baeldung/websocket/client/MyStompSessionHandlerTest.java new file mode 100644 index 0000000000..b8b1ec8c6c --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/websocket/client/MyStompSessionHandlerTest.java @@ -0,0 +1,26 @@ +package com.baeldung.websocket.client; + +import org.baeldung.websocket.client.MyStompSessionHandler; +import org.junit.Test; +import org.mockito.Mockito; +import org.springframework.messaging.simp.stomp.StompHeaders; +import org.springframework.messaging.simp.stomp.StompSession; + +/** + * Test class for MyStompSessionHandler + * @author Kalyan + * + */ +public class MyStompSessionHandlerTest { + + @Test + public void testAfterConnectedSuccessfull() { + StompSession mockSession = Mockito.mock(StompSession.class); + StompHeaders mockHeader = Mockito.mock(StompHeaders.class); + MyStompSessionHandler sessionHandler = new MyStompSessionHandler(); + sessionHandler.afterConnected(mockSession, mockHeader); + Mockito.verify(mockSession).subscribe("/topic/messages", sessionHandler); + Mockito.verify(mockSession).send(Mockito.anyString(), Mockito.anyObject()); + } +} +