mongodb数据库的连接和连接池
This commit is contained in:
parent
28350e8bb1
commit
d4526931e9
|
@ -37,7 +37,6 @@ import java.util.Date;
|
||||||
/**
|
/**
|
||||||
* 定时任务反射工具类
|
* 定时任务反射工具类
|
||||||
*
|
*
|
||||||
* @author 郑健楠
|
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
|
|
|
@ -47,6 +47,7 @@ function connect() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function disconnect() {
|
function disconnect() {
|
||||||
|
sendNameToServer('stop');
|
||||||
stompClient.deactivate();
|
stompClient.deactivate();
|
||||||
// testClient.deactivate();
|
// testClient.deactivate();
|
||||||
setConnected(false);
|
setConnected(false);
|
||||||
|
@ -54,9 +55,13 @@ function disconnect() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendName() {
|
function sendName() {
|
||||||
|
sendNameToServer();
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendNameToServer(status) {
|
||||||
stompClient.publish({
|
stompClient.publish({
|
||||||
destination: "/front/doctorMedicine",
|
destination: "/front/doctorMedicine",
|
||||||
body: $("#name").val()
|
body: status ? status : $("#name").val()
|
||||||
});
|
});
|
||||||
/*testClient.publish({
|
/*testClient.publish({
|
||||||
destination: "/app/test-hello",
|
destination: "/app/test-hello",
|
||||||
|
|
|
@ -30,6 +30,11 @@
|
||||||
<groupId>com.rax</groupId>
|
<groupId>com.rax</groupId>
|
||||||
<artifactId>common-security</artifactId>
|
<artifactId>common-security</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- 引入quartz依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.rax</groupId>
|
||||||
|
<artifactId>quartz</artifactId>
|
||||||
|
</dependency>
|
||||||
<!--日志处理-->
|
<!--日志处理-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.rax</groupId>
|
<groupId>com.rax</groupId>
|
||||||
|
@ -40,6 +45,10 @@
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,66 @@
|
||||||
|
package com.rax.vital.datasource;
|
||||||
|
|
||||||
|
import com.mongodb.ConnectionString;
|
||||||
|
import com.mongodb.MongoClientSettings;
|
||||||
|
import com.mongodb.client.MongoClient;
|
||||||
|
import com.mongodb.client.MongoClients;
|
||||||
|
import org.springframework.data.mongodb.SpringDataMongoDB;
|
||||||
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||||
|
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public class MongoDBSource {
|
||||||
|
private String host;
|
||||||
|
private String password;
|
||||||
|
private String username;
|
||||||
|
private static final String authenticationDatabase = "admin";
|
||||||
|
private String database;
|
||||||
|
private MongoClient mongoClient;
|
||||||
|
|
||||||
|
private static final Map<String, String> datasourceList = new HashMap(300);
|
||||||
|
|
||||||
|
public MongoDBSource(String host, String password, String username, String database) {
|
||||||
|
String url = datasourceList.get(database);
|
||||||
|
if (StringUtils.hasText(url)) {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
this.host = host;
|
||||||
|
this.password = password;
|
||||||
|
this.username = username;
|
||||||
|
this.database = database;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public MongoTemplate open() {
|
||||||
|
MongoClientSettings.Builder mongoBuilder = MongoClientSettings.builder();
|
||||||
|
// mongodb://账户:密码@ip:端口/数据库名?authSource=admin
|
||||||
|
String connectionUrl = "mongodb://" + this.username + ":" + this.password + "@" + this.host + "/" + this.database + "?authSource=admin";
|
||||||
|
mongoBuilder.applyConnectionString(new ConnectionString(connectionUrl));
|
||||||
|
mongoBuilder.applyToConnectionPoolSettings(builder -> {
|
||||||
|
// 允许的最大连接数。
|
||||||
|
builder.maxSize(120);
|
||||||
|
// 最小连接数。
|
||||||
|
builder.minSize(10);
|
||||||
|
// 池连接可以存活的最长时间。零值表示寿命没有限制。超过其生命周期的池连接将被关闭并在必要时由新连接替换
|
||||||
|
builder.maxConnectionLifeTime(0, TimeUnit.SECONDS);
|
||||||
|
// 池连接的最大空闲时间。零值表示对空闲时间没有限制。超过其空闲时间的池连接将被关闭并在必要时由新连接替换
|
||||||
|
builder.maxConnectionIdleTime(6, TimeUnit.MINUTES);
|
||||||
|
// 默认最大连接时间120s;
|
||||||
|
builder.maxWaitTime(60000, TimeUnit.MILLISECONDS);
|
||||||
|
});
|
||||||
|
mongoClient = MongoClients.create(mongoBuilder.build(), SpringDataMongoDB.driverInformation());
|
||||||
|
SimpleMongoClientDatabaseFactory simpleMongoClientDatabaseFactory = new SimpleMongoClientDatabaseFactory(mongoClient, database);
|
||||||
|
MongoTemplate mongoTemplate = new MongoTemplate(simpleMongoClientDatabaseFactory);
|
||||||
|
return mongoTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close() {
|
||||||
|
mongoClient.close();
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,13 +3,15 @@ package com.rax.vital.medicine.controller;
|
||||||
import com.rax.common.core.util.R;
|
import com.rax.common.core.util.R;
|
||||||
import com.rax.vital.medicine.service.AIMedicineService;
|
import com.rax.vital.medicine.service.AIMedicineService;
|
||||||
import com.rax.vital.medicine.service.DoctorMedicineService;
|
import com.rax.vital.medicine.service.DoctorMedicineService;
|
||||||
import com.rax.vital.medicine.vo.MedicineVO;
|
import com.rax.vital.timer.VitalSignTimer;
|
||||||
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.quartz.Scheduler;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.messaging.handler.annotation.MessageMapping;
|
import org.springframework.messaging.handler.annotation.MessageMapping;
|
||||||
import org.springframework.messaging.handler.annotation.SendTo;
|
import org.springframework.messaging.handler.annotation.SendTo;
|
||||||
|
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@ -28,15 +30,23 @@ public class MedicineController {
|
||||||
|
|
||||||
private final DoctorMedicineService doctorMedicineService;
|
private final DoctorMedicineService doctorMedicineService;
|
||||||
|
|
||||||
|
private final Scheduler scheduler;
|
||||||
|
|
||||||
|
private final VitalSignTimer vitalSignTimer;
|
||||||
|
|
||||||
@MessageMapping("/doctorMedicine")
|
@MessageMapping("/doctorMedicine")
|
||||||
@SendTo("/topic/doctorMedicine")
|
public void doctorMedicine(String msg) {
|
||||||
public String doctorMedicine(String msg) {
|
if ("stop".equals(msg)) {
|
||||||
return msg;
|
vitalSignTimer.stopTimerTask("test");
|
||||||
|
} else {
|
||||||
|
vitalSignTimer.createAndSendMessage("test");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* @MessageMapping("/getMedicineFlag")
|
@MessageMapping("/getMedicineFlag")
|
||||||
@SendTo("/topic/medicineFlag")
|
@SendTo("/topic/medicineFlag")
|
||||||
public R getMedicineFlag(String id, String name) {
|
public R getMedicineFlag(String id, String name) {
|
||||||
|
return R.ok();
|
||||||
}*/
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
package com.rax.vital.timer;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class VitalSignTimer {
|
||||||
|
|
||||||
|
private final SimpMessagingTemplate simpMessagingTemplate;
|
||||||
|
private TimerTask timerTask = null;
|
||||||
|
private static final Map<String, TimerTask> timerTaskMap = new HashMap(300);
|
||||||
|
|
||||||
|
public void createAndSendMessage(String database) {
|
||||||
|
TimerTask task = timerTaskMap.get(database);
|
||||||
|
|
||||||
|
if (task != null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Timer timer = new Timer();
|
||||||
|
TimerTask timerTask = new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
// MongoDBSource mongoDBSource = new MongoDBSource("localhost:27017", "root", "root", "ceshi");
|
||||||
|
// MongoTemplate template = mongoDBSource.open();
|
||||||
|
// Query query = new Query();
|
||||||
|
// Criteria criteria = new Criteria();
|
||||||
|
// criteria.where("");
|
||||||
|
// query.addCriteria(criteria);
|
||||||
|
// query.limit(1);
|
||||||
|
// List<Map> cs = template.find(query, Map.class, "cs");
|
||||||
|
try {
|
||||||
|
simpMessagingTemplate.convertAndSend("/topic/doctorMedicine", "111111111");
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
timer.schedule(timerTask, 0, 1000);
|
||||||
|
timerTaskMap.put(database, timerTask);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stopTimerTask(String database) {
|
||||||
|
TimerTask timerTask = timerTaskMap.get(database);
|
||||||
|
if (timerTask != null) {
|
||||||
|
timerTask.cancel();
|
||||||
|
timerTaskMap.remove(database);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user