Scheduled Tasks and Web Socket

Slides:



Advertisements
Similar presentations
Software Technologies
Advertisements

Programming Fundamentals (Extended)
Version Control Systems
Helpers, Data Validation
Auto Mapping Objects SoftUni Team Database Applications
Databases basics Course Introduction SoftUni Team Databases basics
Data Structures Course Overview SoftUni Team Data Structures
C# MVC Frameworks – ASP.NET
ASP.NET Essentials SoftUni Team ASP.NET MVC Introduction
Web API - Introduction AJAX, Spring Data REST SoftUni Team Web API
Introduction to MVC SoftUni Team Introduction to MVC
Deploying Web Application
Setup a PHP + MySQL Development Environment
Thymeleaf and Spring Controllers
WordPress Introduction
PHP MVC Frameworks Course Introduction SoftUni Team Technical Trainers
PHP Fundamentals Course Introduction SoftUni Team Technical Trainers
C# Database Fundamentals with Microsoft SQL Server
JavaScript Applications
ASP.NET Integration Testing
ASP.NET Unit Testing Unit Testing Web API SoftUni Team ASP.NET
Mocking tools for easier unit testing
Parsing JSON JSON.NET, LINQ-to-JSON
JavaScript Applications
State Management Cookies, Sessions SoftUni Team State Management
EF Code First (Advanced)
PHP MVC Frameworks MVC Fundamentals SoftUni Team Technical Trainers
C# Databases Advanced with Microsoft SQL Server
Spring Filters Spring Interceptors SoftUni Team Spring Interceptors
Software Technologies
EF Relations Object Composition
Entity Framework: Code First
Registration, Login, Thymeleaf
Databases advanced Course Introduction SoftUni Team Databases advanced
C#/Java Web Development Basics
MVC Architecture. Routing
Install and configure theme
Entity Framework: Relations
Array and List Algorithms
The Right Way Control Flow
MVC Architecture, Symfony Framework for PHP Web Apps
ASP.NET SignalR SoftUni Team C# MVC Frameworks Technical Trainers
ASP.NET MVC Introduction
JavaScript Fundamentals
C# Advanced Course Introduction SoftUni Team C# Technical Trainers
Databases Advanced Course Introduction SoftUni Team Databases Advanced
C# Web Development Basics
Creating UI elements with Handlebars
Best practices and architecture
Testing with Spring Boot
Design & Module Development
Magento Basics part 2 Modules & Themes Stenik Group Ltd. Magento
Web Fundamentals (HTML and CSS)
Extending functionality using Collections
Exporting and Importing Data
ASP.NET Filters SoftUni Team ASP.NET MVC Introduction
Manual Mapping and AutoMapper Library
ASP.NET Razor Engine SoftUni Team ASP.NET MVC Introduction
C# Advanced Course Introduction SoftUni Team C# Technical Trainers
Course Overview, Trainers, Evaluation
Exporting and Importing Data
JavaScript Fundamentals
CSS Transitions and Animations
Train the Trainers Course
Spring Data Advanced Querying
Version Control Systems
JavaScript Frameworks & AngularJS
JavaScript: ExpressJS Overview
CSS Transitions and Animations
Iterators and Generators
Presentation transcript:

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.