Scheduled Tasks and Web Socket Spring Cookbook Scheduled Tasks and Web Socket Spring Cookbook SoftUni Team Technical Trainers Software University http://softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Table of Contents Scheduled Tasks Web Socket FixedRate FixedDelay Cron SockJS, Stomp JS Sending messages © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
sli.do #JavaWeb Have a Question? © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Scheduled Tasks Creating Crons © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
@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 – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
@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 = "0 0 9-17 * * MON-FRI") public void doStuff() { … } © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
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. "0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day. © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
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 – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Web Socket Dynamic content © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Configuration Enabling Web socket: @EnableWebSocketMessageBroker public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { } © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
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 – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
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 – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
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 – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
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 – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
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 – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
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
Spring Cookbook https://softuni.bg/courses/java-mvc-frameworks-spring © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
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 – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Trainings @ Software University (SoftUni) Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University Foundation softuni.org Software University @ Facebook facebook.com/SoftwareUniversity Software University Forums forum.softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.