Giter VIP home page Giter VIP logo

jackstomp's Introduction

Jackstomp

Maven central version Github Build GitHub license

A tiny wrapper around spring SockJS client to make it easy to use with Jackson-serialized objects on STOMP.

Jackstomp is helpful to either build a client in no time or write integration tests for your Java websocket server.

Basic usage

Jackstomp is here to deal with boilerplate for you. The default JackstompClient simply uses Spring's STOMP websocket client with a Jackson converter. It encapsulates the creation and configuration of all the small elements to put together, so that you can deal with the important stuff instead.

JackstompClient client = new JackstompClient();
JackstompSession session = client.connect("ws://myapp.org/websocket");

MyPojo pojoMsg = new MyPojo(); // any serializable java object

// send your object, serialized to JSON, over the websocket using STOMP
session.send("/app/messages", msg); 

JackstompSession extends StompSession, so you can expect all the usual methods like send() and subscribe().

Active subscriptions for unit tests

Since subscriptions are here for server pushes, you usually need to declare a handler class for the received messages. However, this is not what we want for unit tests, as in this case we know which subscription we expect to receive data on, and thus we want to be able to actively check if something came up.

Jackstomp adds the concept of Channel<T>, which you create when subscribing to a STOMP destination. Just specify the type you expect to receive on the websocket as JSON, and Jackstomp will deal with the handler, the queuing etc. You get a Channel<T> for your type T, which you can then query for data:

JackstompClient client = new JackstompClient();
JackstompSession session = client.connect("ws://myapp.org/websocket");

MyPojo pojoMsg = new MyPojo(); // any serializable java object

// send your object, serialized to JSON, over the websocket using STOMP
Channel<MyPojo> messages = session.subscribe("/topic/messages", MyPojo.class);

MyPojo msg = messages.next(); // blocks until a msg is received, returns null after a default timeout
assertNotNull(msg);
assertEquals(myExpectedPojo, msg);

Request/response pattern

Sometimes, what you need is a basic request/response, but still on websockets. JackstompSession.request() implements this pattern using a subscription and a SEND, expecting a response on the subscribed destination:

User newUser = new User("Bob");

User createdUser = session.request(newUser, User.class, "/app/createUser", "/topic/createdUsers");

assertNotNull(createdUser);
assertEquals(newUser, createdUser);

Which is equivalent to:

User newUser = new User("Bob");

Channel<User> createdUsers = session.subscribe("/topic/createdUsers", User.class);
session.send("/app/createUser", newUser);
User createdUser = createdUsers.next();
createdUsers.unsubscribe();

assertNotNull(createdUser);
assertEquals(newUser, createdUser);

jackstomp's People

Contributors

joffrey-bion avatar

Stargazers

 avatar Piyush Pradeepkumar avatar yinzhidong avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

upupt

jackstomp's Issues

Add java-8-like subscription methods

It would be nice to be able to subscribe to one type of messages, and give a simple handler via a lambda. This interface would look like:

public <T> Subscription subscribe(String destination, Class<T> payloadType, Consumer<T> handler)

Add delegate async connect() methods

Users should be given the possibility to use the basic asynchronous connect() methods that accept handlers as parameters instead of blocking and returning the session.

Allow access to inner websocket client

Wrapping with synchronous versions of the websocket client functions is nice, but we should not prevent the user from using the original asynchronous API.

We could expose the inner client, or add delegate the original methods of the client.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.