BAEL-252(A Java Client to consume a WebSockets API) (#1791)
* @BAEL-252 - Initial checkin * BAEL-252 : Added junit tests
This commit is contained in:
parent
2c790c40cb
commit
4a3662b4ab
|
@ -125,19 +125,31 @@
|
|||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>6.0.6</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-websocket</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-messaging</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.togglz</groupId>
|
||||
<artifactId>togglz-spring-boot-starter</artifactId>
|
||||
<version>${togglz.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.togglz</groupId>
|
||||
<artifactId>togglz-spring-security</artifactId>
|
||||
<version>${togglz.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.activemq</groupId>
|
||||
<artifactId>artemis-server</artifactId>
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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 <code>StompSessionHandlerAdapter</code>.
|
||||
* 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 <code>Message</code>
|
||||
*/
|
||||
private Message getSampleMessage(){
|
||||
Message msg = new Message();
|
||||
msg.setFrom("Nicky");
|
||||
msg.setText("Howdy!!");
|
||||
return msg;
|
||||
}
|
||||
}
|
|
@ -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<Transport> 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.
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue