Spring-Boot without STOMP (raw websocket) - java

Edit: Please do not downvote without leaving at least a comment.
I'd like to implement websocket in a SpringBoot application without Message Broker and STOMP. However, I need to access Spring Beans (repositories and services).
The Java API is so simple that I can't understand why Spring made it so complex.
Here's what I need:
I need to implement a websocket endpoint with dynamic URI so I can use #PathParam like this:
#ServerEndpoint(value = "/chat/{username}")
public class wsEndpoint
Using #EnableWebSocket with WebSocketConfigurer and TextWebSocketHandler only allows me to implement static endpoints.
The alternative recommended by Spring documentation is to implement Message Brokers and STOMP, which makes usage (IMHO) unnecessarily complex for most cases.
I really would like to use #EnableWebSocket with WebSocketConfigurer, TextWebSocketHandler and dynamic URIs. Is there a way to do so?

Related

Spring Cloud Load Balance and Feign Client

I have been using spring-clound-openfeign with Consul as the service registry and Ribbon as Load Balancer. I am currently working with spring-boot 2.3.10.RELEASE.
I really like spring-cloud-feign-inheritance support which, in my understanding, allows me to write a single interface used by the server side and the client side.
https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html#spring-cloud-feign-inheritance
From spring-boot 2.4.x onward, the spring team recommended replacing Ribbon by spring-clould-loadbanced as a replacement since Ribbon is no longer being maintained.
If I have an interface let say:
interface Greeting {
#GetMapping
String hello(String name);
}
By using Spring Openfeign + Ribbon + Consul I could just extend it with:
#FeignClient(name="my-service-id")
interface GreetingClient extends Greeting { }
And with that I would have a client implementation with load balanced capabilities.
Can I still accomplish the same result as spring-cloud-openfeign with spring-clould-loadbalancer or I really need to work with RestTemplate or DiscoveryClient to have the client side of my API?
An over all picture of this would be highly appreciated since nonwhere else I have found a sensible answer.
If you want get work like with ribbon, need connect spring cloud loadbalancer to consul. You can read how connnect spring cloud loadbalanncer to consul into link
https://docs.spring.io/spring-cloud-commons/docs/current/reference/html/#serviceregistry

Web based collaborative editing using AJAX

I am looking to create a web based collaborative editor (like Google docs but very basic. 2 or more users editing a page). I am using the Spring MVC framework and wanted to know the best way to start this.
Should I use AJAX, if so can you point me in the right direction?
Otherwise, how should I go about doing this?
You need spring's websocket support. Go through the spring docs here
You may need to create a handler which will be mapped with websocket in spring like this.
<websocket:handlers>
<websocket:mapping path="/myHandler" handler="myHandler"/>
<websocket:handshake-interceptors>
<bean class="org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor"/>
</websocket:handshake-interceptors>
</websocket:handlers>
Or you can create a web-scoket configuration with annotation like below.
#Configuration
#EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

How can I actually use a JAX-RS client configured with Spring?

My application shall make calls to a REST service. So I added the package cxf-rt-rs-client from org.apache.cxf as a dependency.
In a cxf.xml configuration this is how I define my JAX-RS client:
<jaxrs:client id="myRestClient" serviceClass="org.apache.cxf.jaxrs.client.WebClient">
<http-conf:authorization>
<sec:UserName>testuser</sec:UserName>
<sec:Password>myPassword</sec:Password>
<sec:AuthorizationType>Basic</sec:AuthorizationType>
</http-conf:authorization>
</jaxrs:client>
However I think I have missed a point. How do I actually use this client in ma Java code? There must be some mechanism which makes this myRestClient available in the application?
This is tersely documented in section Injecting proxies of CXF JAX-RS Client API documentation. To be more explicit, the useful Java code for the XML example in this section can be found in the class org.apache.cxf.systest.jaxrs.jaxws.BookStoreSoapRestImpl of CXF samples.
So, in your case, I guess something like this:
#Resource(name = "myRestClient")
private org.apache.cxf.jaxrs.client.WebClient webClient;

Naming Spring Services when accessed by different client types

I am quite new to Spring and have a question related to Spring Service naming convention.
I have written a service, and used an annotation to define and name it.
#Service(value="CustomerService")
This service is implemented within a library that is used by a web app. Everything works fine and I can autowire my service into my client components.
Now I would also like to expose this service using http invoker. This works ok. I have define a /CustomerService http service which accesses CustomerService bean.
The issue I have is that one of my components, a client side component, that I used in my web app (CustomerDetailsValidator) can also be used in this new application.
In my CustomerDetailsValidator I have something like this:
#Autowired
#Qualifier(name="CustomerService").
But if I want to reuse my CustomerDetailsValidator and use it in my new app, this time I need to wire it to the httpservice instead.
Which means that the #Autowired and #Qualifier code is useless.
My question is what is the best practice in this case?
Should I still use #Service?
I guess I cannot use Qualifier anymore.
My feeling is that I should define everything in xml in each application context.
The web app using the library directly would just use the CustomerService bean as a singleton.
While my new client app would link the customer service id to the http service.
Is that a good approach? Do we have patterns for this?
Thanks and regards
Gilles

Manually setting up Spring WebSocket STOMP support

I'm trying to set up a STOMP WS endpoint using spring-websocket and spring-messaging. I am trying to do this manually: no application context is involved at all, and certainly no dispatcher. My goal is to wire up the appropriate Spring components in code inside a ServletContextListener, then register the wired up components directly with the javax.websocket.server.ServerContainer in my JSR 356 compatible container (Tomcat 7). At first, I would like to get this working with the "simple" broker built into spring-messaging; secondly, I would like to implement my own "broker" to directly integrate with an in-process ActiveMQ using the VM transport. This would be in contrast to the STOMP relay which spring-messaging also provides.
The Spring documentation states (http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html):
"...Spring’s WebSocket support does not depend on Spring MVC. It is relatively simple to integrate a WebSocketHandler into other HTTP serving environments with the help of WebSocketHttpRequestHandler."
However, I am not finding it to be simple. Essentially, I started with:
public void contextInitialized(ServletContextEvent sce) {
ServerContainer websocketContainer = (ServerContainer) sce.getServletContext().getAttribute("javax.websocket.server.ServerContainer");
???
websocketContainer.addEndpoint(???);
}
And ended up with an incoherent mess of assorted spring-websocket and spring-messaging constructor invocations which do not compile and are certainly not worth reproducing here.
I realize this is a bit vague, this is because I'm a bit lost! Has anyone done something like this, or has some general guidance to contribute?
Did you try sample application - tests for the stock portfolio. (This link is at the very end of the spring documentation link).
It says
Demonstrates 3 approaches to testing a Spring STOMP over WebSocket application:
Server-side controller tests that load the actual Spring configuration (context sub-package)
Server-side controller tests that test one controller at a time without loading any Spring configuration (standalone sub-package)
End-to-end, full integration tests using an embedded Tomcat and a simple STOMP Java client (tomcat sub-package)
See the Javadoc of the respective tests for more details.
Second option is probably what you need.

Categories

Resources