Download presentation
Presentation is loading. Please wait.
1
Scheduled Tasks and Web Socket
Spring Cookbook Scheduled Tasks and Web Socket Spring Cookbook SoftUni Team Technical Trainers Software University © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
2
Table of Contents Scheduled Tasks Web Socket FixedRate FixedDelay Cron
SockJS, Stomp JS Sending messages © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
3
sli.do #JavaWeb Have a Question?
© Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
4
Scheduled Tasks Creating Crons
© Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
5
@Scheduled How to give resources every 5 seconds:
Execute method based on previous execution time: @Scheduled(fixedRate = 5000) // 5000 milliseconds public void giveGold() { … } @Scheduled(fixedDelay = 5000) // 5000 milliseconds public void doStuff() { … } © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
6
@Scheduled (2) Starting a task after waiting for all services to be initialized: Create complex timer that will execute every hour 9 to 17 on weekdays @Scheduled(initialDelay = 5000, fixedRate = 3000) public void doStuff() { … } @Scheduled(cron = " * * MON-FRI") public void doStuff() { … } © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
7
Crons Crons are date sequence generators
List of six single space-separated fields: representing second, minute, hour, day, month, weekday "0 0 * * * *" = the top of every hour of every day. "*/10 * * * * *" = every ten seconds. " * * *" = 8, 9 and 10 o'clock of every day. © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
8
Cron Syntax Crons expression symbols:
* - Every possible value of the field ? - The field won't be checked (used to avoid conflicts) /X - Skips every Xth value X-Y - Interval with all values between X and Y X,Y,Z - List with the given values © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
9
Web Socket Dynamic content
© Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
10
Configuration Enabling Web socket: @EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { } © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
11
Creating End-Point STOMP is used to send text-based messages. STOMP is layer on top of TCP: SockJS will pick the best transport available (websocket, xhr- streaming, xhr-polling, etc.). @Override public void registerStompEndpoints( StompEndpointRegistry registry) { registry.addEndpoint("/game").withSockJS(); } © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
12
Creating Message Broker
STOMP is used to send text-based messages. STOMP is layer on top of TCP: @Override public void configureMessageBroker( MessageBrokerRegistry config) { config.enableSimpleBroker( "/character/money", "/character/info"); } © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
13
Sending Messages Spring provides an easy way to send messages:
By default, objects will be converted to JSON. @Autowired private final SimpMessagingTemplate template; public void sendMoneyUpdate(CharacterMoneyModel character) { this.template.convertAndSend( "/character/money", character); } © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
14
Receiving Messages with JS
Download or find a link to SockJS and StompJS. Connecting to an end-point: let socket = new SockJS('/game'); // /game is an end-point let stompClient = Stomp.over(socket); stompClient.connect({}, function (frame) { //Subscribe to message collections }); © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
15
Receiving Messages with JS (2)
Subscribing to message collections: stompClient.connect({}, function (frame) { stompClient.subscribe('/character/money', function (data) { // /character/money is the simple message broker we defined earlier let obj = JSON.parse(data.body); // Do something with the data }); © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
16
Summary You can schedule tasks in order to automate processes in your application There are multiple ways to schedule a task using fixedRate, fixedDelay, cron, etc. Web sockets can be used to dynamically change content of pages
17
Spring Cookbook https://softuni.bg/courses/java-mvc-frameworks-spring
© Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
18
License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
19
Trainings @ Software University (SoftUni)
Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University Foundation softuni.org Software Facebook facebook.com/SoftwareUniversity Software University Forums forum.softuni.bg © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.