How To Use Threads In Java
Today, I want to talk about threads in Angular. Sometimes our services takes much longer time than we expected. And the service could get time out error. We must use services with threads in order to prevent from time out errors.
Let’s start with writing our web service with a thread. As you see below, our service works first and give a quick response and thread runs in background.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
// imports.. @Service @Transactional public class FooService { // dependencies.... @Autowired FooRepository fooRepository; public Foo findById(Long fooId) { return fooRepository.findOne(Long fooId); } // other methods.. public Foo updateStatus(Foo foo) { some logic... Foo fooInstance = foo; Thread th = new Thread() { @Override public void run() { // some complex logic and transactions in thread.... fooInstance.status = “ACTIVE”; fooRepository.save(fooInstance); } } th.start(); return fooInstance; } } |
Now we can write the front end side code with angular. Firstly we will define our services in the ts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
foo-service.ts import { Injectable } from '@angular/core'; import { HttpClient, HttpResponse, HttpHeaders } from '@angular/common/http'; import { Observable, BehaviorSubject } from 'rxjs'; import { Foo, IFoo } from './foo-model'; // other imports... @Injectable({ providedIn: 'root' }) export class FooService { private resourceUrl = 'http://foo-demo.net/'; constructor(private http: HttpClient) { } findById(id: number): Observable<HttpResponse<IFoo>> { return this.http.get<IFoo>(`${this.resourceUrl + ‘api/find-by-id’}/${id}`, { observe: 'response' }); } update(foo: IFoo): Observable<HttpResponse<IFoo>> { return this.http .post<IFoo>(this.resourceUrl + '/api/updateStatus', foo, { observe: 'response' }); } } |
Then we will use service methods in our component ts file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
foo-component.ts import { Component, OnInit, OnDestroy, ViewChild } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { HttpResponse, HttpErrorResponse, HttpHeaders } from '@angular/common/http'; import { Observable, interval, Subscription } from 'rxjs'; import { FooService } from './foo-service'; import { Foo, IFoo } from './foo-model'; other imports ..... @Component({ selector: 'foo-component', templateUrl: './foo.component.html', providers: [] }) export class FooComponent implements OnInit, OnDestroy { private fooSubscription: Subscription; some variables... constructor( private fooService: FooService, other services... } ngOnInit() { // some code in init... this.fooSubscription = interval(1000).subscribe( (res: number) => { if (res % 60 === 0 && this.foo.status === ‘PASSIVE’) { this.fooService.findById(this.foo.id).subscribe((resp: HttpResponse<IFoo>) => { this.foo = resp.body; if (this.foo.status === ‘ACTIVE’) { alert(‘Foo is active’); } }); } }, (err: HttpErrorResponse) => console.error(err.message) ); } updateFoo() { some logic in frontend... this.fooService.update(this.foo).subscribe((resp: HttpResponse<IFoo>) => { this.foo = resp.body; }); } } |
Then we call the service in the component ts code. We will renew page per 1 second and control the service status 60 seconds time interval. That’s so easy and fast coding. If you have any questions about this post or any other subjects just mail to me.
Smart logic, thank you!