| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,13 +1,12 @@ | |||
| 1 | - package info.xiaomo.anysc; | ||
| 1 | + package info.xiaomo.anysc | ||
| 2 | 2 | ||
| 3 | 3 | ||
| 4 | - import org.springframework.boot.SpringApplication; | ||
| 5 | - import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
| 6 | - import org.springframework.boot.autoconfigure.domain.EntityScan; | ||
| 7 | - import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | ||
| 8 | - import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; | ||
| 9 | - import org.springframework.context.annotation.ComponentScan; | ||
| 10 | - import org.springframework.context.annotation.Configuration; | ||
| 4 | + import org.springframework.boot.SpringApplication | ||
| 5 | + import org.springframework.boot.autoconfigure.EnableAutoConfiguration | ||
| 6 | + import org.springframework.boot.autoconfigure.domain.EntityScan | ||
| 7 | + import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration | ||
| 8 | + import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration | ||
| 9 | + import org.springframework.context.annotation.ComponentScan | ||
| 11 | 10 | ||
| 12 | 11 | /** | |
| 13 | 12 | * 把今天最好的表现当作明天最新的起点..~ | |
@@ -18,18 +17,16 @@ | |||
| 18 | 17 | * @author : xiaomo | |
| 19 | 18 | * github: https://github.com/xiaomoinfo | |
| 20 | 19 | * email: xiaomo@xiaomo.info | |
| 21 | - | ||
| 20 | + * | ||
| 22 | 21 | * Date: 2016/4/1 15:38 | |
| 23 | 22 | * Description: RabbitMq启动器 | |
| 24 | 23 | * Copyright(©) 2015 by xiaomo. | |
| 25 | - **/ | ||
| 26 | - @Configuration | ||
| 27 | - @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class}) | ||
| 24 | + */ | ||
| 25 | + @EnableAutoConfiguration(exclude = arrayOf(DataSourceAutoConfiguration::class, HibernateJpaAutoConfiguration::class)) | ||
| 28 | 26 | @ComponentScan("info.xiaomo") | |
| 29 | 27 | @EntityScan("info.xiaomo.*.model") | |
| 30 | - public class AsyncMain { | ||
| 31 | - public static void main(String[] args) throws Exception { | ||
| 32 | - SpringApplication.run(AsyncMain.class, args); | ||
| 33 | - } | ||
| 28 | + class AsyncMain | ||
| 34 | 29 | ||
| 30 | + fun main(args: Array<String>) { | ||
| 31 | + SpringApplication.run(AsyncMain::class.java, *args) | ||
| 35 | 32 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,54 @@ | |||
| 1 | + package info.xiaomo.anysc.controller | ||
| 2 | + | ||
| 3 | + import info.xiaomo.anysc.task.AsyncTask | ||
| 4 | + import info.xiaomo.core.base.Result | ||
| 5 | + import org.springframework.beans.factory.annotation.Autowired | ||
| 6 | + import org.springframework.web.bind.annotation.RequestMapping | ||
| 7 | + import org.springframework.web.bind.annotation.RequestMethod | ||
| 8 | + import org.springframework.web.bind.annotation.RestController | ||
| 9 | + | ||
| 10 | + /** | ||
| 11 | + * 把今天最好的表现当作明天最新的起点..~ | ||
| 12 | + * いま 最高の表現 として 明日最新の始発..~ | ||
| 13 | + * Today the best performance as tomorrow newest starter! | ||
| 14 | + * Created by IntelliJ IDEA. | ||
| 15 | + * | ||
| 16 | + * @author : xiaomo | ||
| 17 | + * github: https://github.com/xiaomoinfo | ||
| 18 | + * email: xiaomo@xiaomo.info | ||
| 19 | + * | ||
| 20 | + * | ||
| 21 | + * Date: 2016/11/15 15:12 | ||
| 22 | + * Description: 用户实体类 | ||
| 23 | + * Copyright(©) 2015 by xiaomo. | ||
| 24 | + */ | ||
| 25 | + | ||
| 26 | + @RestController | ||
| 27 | + @RequestMapping("/") | ||
| 28 | + class TestController @Autowired | ||
| 29 | + constructor(private val task: AsyncTask) { | ||
| 30 | + | ||
| 31 | + @RequestMapping(value = "/", method = arrayOf(RequestMethod.GET)) | ||
| 32 | + @Throws(Exception::class) | ||
| 33 | + fun task(): Result<*> { | ||
| 34 | + val start = System.currentTimeMillis() | ||
| 35 | + | ||
| 36 | + val task1 = task.doTaskOne() | ||
| 37 | + val task2 = task.doTaskTwo() | ||
| 38 | + val task3 = task.doTaskThree() | ||
| 39 | + | ||
| 40 | + while (true) { | ||
| 41 | + if (task1.isDone && task2.isDone && task3.isDone) { | ||
| 42 | + // 三个任务都调用完成,退出循环等待 | ||
| 43 | + break | ||
| 44 | + } | ||
| 45 | + Thread.sleep(1000) | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + val end = System.currentTimeMillis() | ||
| 49 | + | ||
| 50 | + println("任务全部完成,总耗时:" + (end - start) + "毫秒") | ||
| 51 | + return Result(end - start) | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,53 @@ | |||
| 1 | + package info.xiaomo.anysc.task | ||
| 2 | + | ||
| 3 | + import org.springframework.scheduling.annotation.Async | ||
| 4 | + import org.springframework.scheduling.annotation.AsyncResult | ||
| 5 | + import org.springframework.stereotype.Component | ||
| 6 | + import java.util.* | ||
| 7 | + import java.util.concurrent.Future | ||
| 8 | + | ||
| 9 | + /** | ||
| 10 | + * @author : xiaomo | ||
| 11 | + */ | ||
| 12 | + @Component | ||
| 13 | + open class AsyncTask { | ||
| 14 | + | ||
| 15 | + @Async | ||
| 16 | + @Throws(Exception::class) | ||
| 17 | + open fun doTaskOne(): Future<String> { | ||
| 18 | + println("开始做任务一") | ||
| 19 | + val start = System.currentTimeMillis() | ||
| 20 | + Thread.sleep(random.nextInt(10000).toLong()) | ||
| 21 | + val end = System.currentTimeMillis() | ||
| 22 | + println("完成任务一,耗时:" + (end - start) + "毫秒") | ||
| 23 | + return AsyncResult("任务一完成") | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + @Async | ||
| 27 | + @Throws(Exception::class) | ||
| 28 | + open fun doTaskTwo(): Future<String> { | ||
| 29 | + println("开始做任务二") | ||
| 30 | + val start = System.currentTimeMillis() | ||
| 31 | + Thread.sleep(random.nextInt(10000).toLong()) | ||
| 32 | + val end = System.currentTimeMillis() | ||
| 33 | + println("完成任务二,耗时:" + (end - start) + "毫秒") | ||
| 34 | + return AsyncResult("任务二完成") | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + @Async | ||
| 38 | + @Throws(Exception::class) | ||
| 39 | + open fun doTaskThree(): Future<String> { | ||
| 40 | + println("开始做任务三") | ||
| 41 | + val start = System.currentTimeMillis() | ||
| 42 | + Thread.sleep(random.nextInt(10000).toLong()) | ||
| 43 | + val end = System.currentTimeMillis() | ||
| 44 | + println("完成任务三,耗时:" + (end - start) + "毫秒") | ||
| 45 | + return AsyncResult("任务三完成") | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + companion object { | ||
| 49 | + | ||
| 50 | + private val random = Random() | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments