How to access spring mvc properties value in websocket onopen event? - java

I am using spring mvc + websocket. I have created application.properties to store configuration data of project. Now I want to use that properties values in websockets #onOpent() event. I am able to access those properties in simple rest controller #RequestMapping() but unfortunately I am not able to access those properties in in websockets #onOpent() event, I am getting null value for the same. How can I achieve this?

This happens because Websocket server class in not spring component.
you can mark websocket handler class as component using #Component annotation on class. Then you will be able to access properties.
Hope this helps you.

Seems like you are using Java websockets. you can use spring 4 websockets here is sample

Related

Is it possible to listen to multiple TIbco EMS queues in Spring boot?

I am trying to create multiple queue receivers based on the queueNames from the database.
I have tried creating a JpaRepository and in #Configuration class Autowired it. But this approach is throwing error. Is there any other way to accomplish this?
Note: I am using Spring Boot.
In spring is only #JmsListener annotation but you can't change it dynamically.
There is only one way. You can get list of topics/queues from database and after that poll topics/queues in a loop.

Spring config #RefreshScope

Can I use #RefreshScope (along with #Value on a property) only in Cloud config server or can I use without config server as well? I'm trying to use it without config server. I am trying to fetch #Value property by changing value in a .property file and trying to request again, will I get updated value? Is that possible?
No, you should use it along with Config server otherwise you won't be able to read the update properties on fly. Follow this article and have a look into this if you face any issue loading updated properties dynamically.
In theory, you could refresh the application context, but I wouldn't
recommend this. Spring Cloud has provided an annotation to mark a bean
as refreshable. By adding spring actuator, we can refresh those beans
on the fly.

Dynamic scheduling with properties in spring boot

I am using Spring Boot and have an issue related #Scheduled.
#Scheduled(fixedRateString = "${timeRate}")
public void SaveStatisticData() {
System.out.println("save Info per "+timeRate+"msec");
...
}
this is a part of my code. this method will run by timeRate value from properties and Properties File is connected with web API.
That means someone can request to change timeRate in Properties File.
I want to know how to reload spring boot on client request which change Properties File.
AFAIK, I can use Hot Swapping in spring boot.
But I think this solution is not good to service.
Because The service have to reload whenever a client request changing timeRate.
In other words, is there another way to apply this client request in real time?

What is the right approach for extending Spring Cloud Config Client?

I want to replace Basic Authentication for Spring Cloud Config Server with oAuth implementation. Let's leave Config Server alone for now and focus on changes for Config Client. Obviously I don't want to write my own implementation for whole thing, but instead execute my own logic and fallback on standard Config Client. Also I have to pack my changes into library since I will use it in multiple micro-services.
Long story short I want to achieve following:
1a. Create custom Starter which will contain Spring Cloud Config Client as dependency. Is it even doable or necessary?
or
1b. Create custom Starter with only my custom logic which will be executed before Spring Cloud Config Client. In this case each micro-service will have Spring Cloud Config Client and custom Starter as dependencies. How can I manage execution order and inject custom logic results into Config Client?
2.Introduce new bootstrap settings. e.g. spring.cloud.config.custom.username and spring.cloud.config.custom.password (Optional).
3.Introduce custom annotation for custom Starter. e.g. #enableCustomConfigClient (Optional).
I started with building custom Starter with following code in /resources/META-INF/spring.factories:
# Bootstrap components
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.example.greeter.config.ConfigClientBootstrapConfiguration
But this code invoked after profile is set, not the first thing like Config Client does.
Any suggestions and especially code samples are appreciated. Thanks!
Posting approach I chose for future reference.
Create new package which will be executed on top of / before Spring Cloud Config Client. Two main features here:
Create file src/main/resources/META-INF/spring.factories with org.springframework.cloud.bootstrap.BootstrapConfiguration={YOUR_CLASS}
In {YOUR_CLASS} apply custom logic. Don't forget to use #org.springframework.core.annotation.Order({YOUR_PRECEDENCE}) and fact that Ordered.LOWEST_PRECEDENCE will be executed first
Build jar from previous step and include it into your project (as local file or via artifactory)
Add Custom logic to Spring Cloud Config Server so it can use JWT.
Working example is here: https://github.com/ka4ok85/spring-cloud-config-client-jwt

Java websocket trigger event to javascript client

I'm building an application with Spring 3, Spring MVC, Tomcat7.
I'm triying to refresh content in a jsp when something changes in server side.
For this purpose I followed this tutorial: https://netbeans.org/kb/docs/javaee/maven-websocketapi.html and this one http://self-learning-java-tutorial.blogspot.com.es/2014/08/annotated-server-endpoint.html.
Now I want to trigger one message to all clients (want to see the message in all oppened jsp pages) but I'm not sure how to do this. (For example when an entity is removed in server-side)
As far as I undesrstand, will I need to trigger somehow the method annotated with #OnMessage? and how would I do this? maybe injecting this #ServerEndpoint... class?
Thanks you all

Categories