Integrating JaxRS REST service with WebSockets - java

I'm attempting to develop a social network that implements publisher-subscribe pattern (kind of like Twitter does): users can follow people, therefore being notified when a new publication of their followers is sent.
All I have now is a working REST service implemented with JaxRS, running over Tomcat 7, offering services for login, register, getting profiles data and submitting posts. Servlet Mapping is done via web.xml
My JaxRS app config
RestServices
Web.xml
But the weight of the application comes from the pubsub part. And here is where things become messy.
Before speaking of technologies and implementation, the life cycle of this pubsub:
Let be A and B some users of the app and A follows (is subscribed to) B.
A navigates to his "timeline", therefore registering a WebSocket endpoint in the server from which he will receive notifications when any of the people he's suscribed to publishes anything. (A, himself, would be the topic?)
B publishes a new post, it's sent to JaxRS service via HTTP POST /post.
Server stores publication in database and then sends it to listening subscribers via WebSocket. As A is alive and listening, the publication is sent through his WebSocket/topic.
A gets notified of the new publication.
I've worked with Java built-in WebSockets (javax.websocket), Spring websockets (via STOMP) and JavaScript either built-in and SockJS. I know JaxRS has something called Server Sent Events that act like websockets but I'm absolutely lost on how to initialize and work with them.
My doubts:
1) Could I "mix" e.g Spring Websockets with my JaxRS server and how would it be initialized?
2) With JaxRS and SSE, can I dynamically create WS endpoints on the server?
Thanks in advance

Finally made a workaround using Jersey's Server Sent Events, though not supported by IE, but works for what I needed
Here is the code for the SSEProvider and SSEDispatcher
Gradle dependency
And front end connection and handlers
Also some docs on HTML5 SSE

Related

Spring AMQP RPC vs Spring AMQP Request/Reply Messaging

I have been learning how to use spring framework and so far so good. I have created an app from Spring Initializr - http://start.spring.io/ and i have been able to setup numerous controllers and even setup security and everything works out.
I am running my app on Apache Tomcat. I now want to use Spring AMQP and the way i want to use it is like this. I shall execute a controller method in the browser and that adds a message in the queue which should be consumed by a consumer,possibly a threadpool powered consumer and return the result to the user.
For instance i want to add a message to the queue and return json to the user.
Since i executed the add message from a controller, i am expecting to show the response to the user.
After going through the rabbit mq docs, i think RPC would help me publish the message to the queue and it shall be consumed and the result returned to teh user. I setup the rabbit mq example using php and i saw it work.
However, upon looking through the spring amqp docs,i came across this three
-Message Listener https://docs.spring.io/spring-amqp/docs/1.3.5.RELEASE/reference/html/amqp.html#containerAttributes
-Listener Concurrency https://docs.spring.io/spring-amqp/docs/1.3.5.RELEASE/reference/html/amqp.html#listener-concurrency
-Request/Reply Messaging https://docs.spring.io/spring-amqp/docs/1.3.5.RELEASE/reference/html/amqp.html#request-reply
-RPC https://www.rabbitmq.com/tutorials/tutorial-six-spring-amqp.html
My question is,if i wanted to let the user get the result, would i pick RPC or Request/Reply Messaging or are they the same thing?
Second, in the last RPC example i did, i started the listener separately like java_listener.java to be able to consume the messages.
Since i started my app with spring intilizr and i am running tomcat, will i need to start the consumer separately from the tomcat process?.
Thanks.

How to implement Synchronous web service on JMS / MOM system?

I am working on a project which have many legacy heterogeneous systems. We are planing to connect them using JMS/MOM/ESB but need Synchronous web service calls from client.
i.e Request/Response architecture using web service is a requirement.
Client will make calls and wait for response.
My question is how can we implement Request/ response system which internally work on JMS/MOM to connect desperate systems?
Second question : Do any existing JMS/MOM or ESB implementations support such synchronous architecture?
first about second question: All of them support such architecture.
second about first:-)
just a brief idea:
when you send a message to target JMS Queue you have to have a unique message ID (in headers as example) then target system must answer (response) to "replyTo" queue. Then you have to have listener on that replyTo queue with filter by that unique ID and your flow must wait for response from that listener.
Something like that...

How RefreshRemoteApplicationEvent is handled?

The documentation says that when spring cloud config server detects configuration chages it fires an RefreshRemoteApplicationEvent. But documentation said nothing about how that event is handled. So is it true that each application which receive such event shoud handle it by itself? E.g it is not required to refresh entire Spring context when such event was received?
I think the documentation only talks about the server side, i.e. the Spring application that talks to the git repository and exposes the condensed information to interested clients. In this process, for example using webhooks, the server can be informed about changes in the git repository, and in turn sends out events to applications that might need to be re-configured.
Your question seems to be concerned about the client side. If your application uses Spring Cloud Config, it should automatically request the new configuration data as soon as the event described above arrives at the client. This in turn should mean that the new configuration values are available or some configured behaviour (log level?) changes.
To actually make the server fire an event that arrives at the client, the documentation suggests Spring Cloud Bus. If you create (for example) a RabbitMQ instance, and make this available to both your clients and your server, Spring automatically attaches to this system and is able to process messages. Additionally, the Spring Cloud Config server automatically sends the desired events using this system, and the clients automatically process these.
In short, if you add Spring Cloud Bus to all involved applications (and make the system used by it, e.g. RabbitMQ, available to them), everything works as expected.

Spring 3: Network Client-Server with custom protocol for runtime service generation

I am looking for a simplest solution to create a client-server network architecture using Spring 3 framework. The architecture woill have many clients and multiple servers. Each client can connnect to each server. Each client can define a set of services that would have to be generated during runtime by the server.
Communication protocol:
Client says hello to one of 5 servers.
Server gather its local metadata about stored data and send it to client
Client pick some of this info and send the metadata subset to server deciding which data it will need later.
Server basing on the metadata choice, picked by the client, generates dynamically services that will be made available to the client supplying him with data pointed by the requested (step 3) config (eg in form serialized JSON)
Client get the information about generated services and use it for future calls to those services.
The biggest issue is that client doesn't know nothing about server resources to be served until it receives answer and server has no services since it get request from client.
I considered Spring 3:
HTTP Invokers
JMS
Netty (joined with spring)
But as far as I tried the above it's ether hard to provide the dynamic service generation requirement or the amount of code (Netty) is big.
I have rejected SOAP due to its heavy nature.
On the other hand REST does not bring here as far as I know any benefits. It is just a way of serving data and it require some kind of servlet container like Tomcat as it uses HTTP. #Timmmm 's great and simple answer to REST fashion
What I am after:
as simple as possible
dynamic generation of services based on client choice
keep server lightweight i.e. no additional server instance (it would be nice to eliminate tomcat; but ts not crucial)
spring based
What technology would you recommend?
It is quiet hard to accomplish this task with the requirement of configuration based service generation during runtime.
I do NOT want to base on properties files, services must be generated on the fly based on the client request.
Thank you in advance for answers and tips.
I would look at RESTful architecture. Some of its principals is what you are after, including discovery.
Spring provides easy integration with REST.

Using Spring in Java swing app

We are seeing Spring in school right now but we don't have the time to wait till the end of the semester to start developing an application. We continue using an app we made last year, and are writing the service layer right now.
The problem is our "client" wants to have a desktop client and a webpart, which used the same dtatabase. This would be no problem if we hook up a server that can handle RMI. So basically we want to be able to retrieve/send data to the server that runs our service layer, and use the objects on the client side as well.
I have no idea where to start digging in Spring to figure out how to do this, so some help would be appreciated.
PS: At this point I do not need MVC yet. MVC is handled from within the desktop app where we have views and controllers.The model is the same from the one on the service layer. How do we use the same model without copying it?
Check out spring remoting: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/remoting.html
It's easy to expose your spring beans remotely, using a variety of protocols.
You might want to take a look at the REST paradigms. With this in mind you could have a web server running your server part of the application and communicating with clients through the HTTP protocol. A simple client could be a webpage in the browser which gets the corresponding HTML pages from the server, or a Swing client which communicates over JSON with the server.
The server can implement different methods for JSON or HTML communication and the server can decide what implementation to use by looking at the Accept Header of the Request objects sent to it, that's what they call Content Negotiation
JSR-311 is implemented as Project Jersey which is a framework for RESTful webservices. You might want to take a look at that.
hope that helped

Categories

Resources