parent
356b3cc5a8
commit
1d5c0e8e2f
@ -0,0 +1,32 @@
|
||||
package com.mashibing.controller;
|
||||
|
||||
import com.mashibing.service.Hippo4jService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author zjw
|
||||
* @description
|
||||
*/
|
||||
@RestController
|
||||
public class Hippo4jController {
|
||||
|
||||
|
||||
@Autowired
|
||||
private Hippo4jService service;
|
||||
|
||||
|
||||
@GetMapping("/cpu")
|
||||
public String cpu() throws InterruptedException {
|
||||
Long result = service.doSomeCPUThing();
|
||||
return result + "";
|
||||
}
|
||||
|
||||
@GetMapping("/io")
|
||||
public String io() throws Exception {
|
||||
String result = service.doIOSomeThing();
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@ -1,19 +1,16 @@
|
||||
package com.mashibing.mq;
|
||||
|
||||
import com.mashibing.controller.TestController;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author zjw
|
||||
* @description
|
||||
*/
|
||||
@Component
|
||||
public class TestListener {
|
||||
|
||||
@RabbitListener(queues = "hippo4j",containerFactory = "defaultContainerFactory")
|
||||
public void consume(String message){
|
||||
System.out.println(Thread.currentThread().getName() + ":消费消息 --> " + message);
|
||||
}
|
||||
|
||||
}
|
||||
//package com.mashibing.mq;
|
||||
//
|
||||
//import com.mashibing.controller.TestController;
|
||||
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
//import org.springframework.stereotype.Component;
|
||||
//
|
||||
//
|
||||
//@Component
|
||||
//public class TestListener {
|
||||
//
|
||||
// @RabbitListener(queues = "hippo4j",containerFactory = "defaultContainerFactory")
|
||||
// public void consume(String message){
|
||||
// System.out.println(Thread.currentThread().getName() + ":消费消息 --> " + message);
|
||||
// }
|
||||
//
|
||||
//}
|
||||
|
@ -0,0 +1,11 @@
|
||||
package com.mashibing.service;
|
||||
|
||||
/**
|
||||
* @author zjw
|
||||
* @description
|
||||
*/
|
||||
public interface Hippo4jService {
|
||||
String doIOSomeThing() throws Exception;
|
||||
|
||||
Long doSomeCPUThing() throws InterruptedException;
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
package com.mashibing.service.impl;
|
||||
|
||||
import com.mashibing.service.Hippo4jService;
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
* @author zjw
|
||||
* @description
|
||||
*/
|
||||
@Service
|
||||
public class Hippo4jServiceImpl implements Hippo4jService {
|
||||
|
||||
@Resource
|
||||
private ThreadPoolExecutor ioThreadPool;
|
||||
|
||||
@Resource
|
||||
private ThreadPoolExecutor cpuThreadPool;
|
||||
|
||||
@Override
|
||||
public String doIOSomeThing() throws Exception{
|
||||
CountDownLatch latch = new CountDownLatch(3);
|
||||
Future<String> job1Result = ioThreadPool.submit(() -> {
|
||||
String result1 = job1();
|
||||
latch.countDown();
|
||||
return result1;
|
||||
});
|
||||
Future<String> job2Result = ioThreadPool.submit(() -> {
|
||||
String result2 = job2();
|
||||
latch.countDown();
|
||||
return result2;
|
||||
});
|
||||
Future<String> job3Result = ioThreadPool.submit(() -> {
|
||||
String result3 = job3();
|
||||
latch.countDown();
|
||||
return result3;
|
||||
});
|
||||
latch.await();
|
||||
return job1Result.get() + job1Result.get() + job1Result.get();
|
||||
}
|
||||
@SneakyThrows
|
||||
private String job1() {
|
||||
Thread.sleep(100);
|
||||
return "RedisResult!";
|
||||
}
|
||||
@SneakyThrows
|
||||
private String job2() {
|
||||
Thread.sleep(200);
|
||||
return "ServiceResult!";
|
||||
}
|
||||
@SneakyThrows
|
||||
private String job3() {
|
||||
Thread.sleep(200);
|
||||
return "MySQLResult!";
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Long doSomeCPUThing() throws InterruptedException {
|
||||
AtomicLong atomicLong = new AtomicLong(0);
|
||||
CountDownLatch latch = new CountDownLatch(2);
|
||||
cpuThreadPool.execute(() -> {
|
||||
incr(atomicLong);
|
||||
latch.countDown();
|
||||
});
|
||||
cpuThreadPool.execute(() -> {
|
||||
incr(atomicLong);
|
||||
latch.countDown();
|
||||
});
|
||||
latch.await();
|
||||
return atomicLong.get();
|
||||
}
|
||||
private void incr(AtomicLong atomicLong){
|
||||
for (int i = 0; i < 5000000; i++) {
|
||||
atomicLong.incrementAndGet();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
spring:
|
||||
application:
|
||||
name: hippo4j-client
|
||||
dynamic:
|
||||
thread-pool:
|
||||
server-addr: 192.168.11.88:6691
|
||||
username: admin
|
||||
password: 123456
|
||||
namespace: mashibing # 租户名称
|
||||
item-id: ${spring.application.name} # 项目名称,需要与与服务名称保持一致。
|
||||
rabbitmq:
|
||||
host: 192.168.11.88
|
||||
port: 5672
|
||||
username: guest
|
||||
password: guest
|
||||
virtual-host: /
|
@ -0,0 +1,10 @@
|
||||
spring:
|
||||
application:
|
||||
name: hippo4j-client
|
||||
dynamic:
|
||||
thread-pool:
|
||||
server-addr: localhost:6691
|
||||
username: admin
|
||||
password: 123456
|
||||
namespace: mashibing # 租户名称
|
||||
item-id: ${spring.application.name} # 项目名称,需要与与服务名称保持一致。
|
@ -1,18 +1,3 @@
|
||||
spring:
|
||||
profiles:
|
||||
active: dev
|
||||
application:
|
||||
name: hippo4j-client
|
||||
dynamic:
|
||||
thread-pool:
|
||||
server-addr: 192.168.11.88:6691
|
||||
username: admin
|
||||
password: 123456
|
||||
namespace: mashibing # 租户名称
|
||||
item-id: ${spring.application.name} # 项目名称,需要与与服务名称保持一致。
|
||||
rabbitmq:
|
||||
host: 192.168.11.88
|
||||
port: 5672
|
||||
username: guest
|
||||
password: guest
|
||||
virtual-host: /
|
||||
active: dev
|
Loading…
Reference in new issue