Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from igakilab/sample5-1
sample5-1
- Loading branch information
Showing
4 changed files
with
92 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/jp/ac/igakilab/springbootsamples/controller/Sample5RestSseController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package jp.ac.igakilab.springbootsamples.controller; | ||
|
||
import java.util.Date; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import jp.ac.igakilab.springbootsamples.service.AsyncService; | ||
|
||
/** | ||
* Sample5SseController | ||
*/ | ||
@RestController | ||
@RequestMapping("sample5") | ||
public class Sample5RestSseController { | ||
|
||
@Autowired // このアノテーションがついていると,対象のクラスのオブジェクトをSpringbootが管理し,自動的にnew等してくれる.ただし,対象のクラスに@Serviceや@Component等Springbootでの管理対象であることを示すアノテーションがついている必要がある. | ||
AsyncService asyncService; | ||
|
||
@GetMapping("sample51") | ||
public String sample51() { | ||
Date date = new Date(); | ||
System.out.println(date); | ||
asyncService.async(); | ||
return date.toString(); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/jp/ac/igakilab/springbootsamples/service/AsyncService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package jp.ac.igakilab.springbootsamples.service; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* AsyncService 一般的にビジネスロジックなどを記述するクラスをサービスとする | ||
* | ||
* @Serviceをつけることで,Springbootが管理してくれるように鳴る | ||
*/ | ||
@Service | ||
public class AsyncService { | ||
|
||
@Async | ||
public void async() { | ||
System.out.println("async start"); | ||
try { | ||
TimeUnit.SECONDS.sleep(5); | ||
} catch (InterruptedException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
System.out.println("async end"); | ||
} | ||
} |