Skip to content

Commit c6549b5

Browse files
committed
async java to kotlin
1 parent 99e7a01 commit c6549b5

File tree

5 files changed

+120
-224
lines changed

5 files changed

+120
-224
lines changed
Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
package info.xiaomo.anysc;
1+
package info.xiaomo.anysc
22

33

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
1110

1211
/**
1312
* 把今天最好的表现当作明天最新的起点..~
@@ -18,18 +17,16 @@
1817
* @author : xiaomo
1918
* github: https://github.com/xiaomoinfo
2019
* email: xiaomo@xiaomo.info
21-
20+
*
2221
* Date: 2016/4/1 15:38
2322
* Description: RabbitMq启动器
2423
* Copyright(©) 2015 by xiaomo.
25-
**/
26-
@Configuration
27-
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
24+
*/
25+
@EnableAutoConfiguration(exclude = arrayOf(DataSourceAutoConfiguration::class, HibernateJpaAutoConfiguration::class))
2826
@ComponentScan("info.xiaomo")
2927
@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
3429

30+
fun main(args: Array<String>) {
31+
SpringApplication.run(AsyncMain::class.java, *args)
3532
}

async/src/main/java/info/xiaomo/anysc/controller/TestController.java

Lines changed: 0 additions & 160 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

async/src/main/java/info/xiaomo/anysc/task/AsyncTask.java

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

0 commit comments

Comments
 (0)