Teknologi pengumpulan objek Java GenericObjectPool--Kelas alat kolam sambungan sftp SpringBoot
Kolam objek mengandungi set objek yang telah dimulakan dan boleh digunakan, dan objek boleh dibuat dan dimusnahkan apabila diperlukan. Pengguna kolam boleh mendapatkan objek daripada kolam, memanipulasinya dan mengembalikannya ke kolam apabila tidak diperlukan lagi dan bukannya memusnahkannya secara langsung. Ini adalah jenis objek kilang yang istimewa.
Penggunaan teknologi pengumpulan objek BasePooledObjectFactory
Pom.xml
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.7.0</version> </dependency>
MqttConnection.java
rreeeRreee
Rreee
package com.vipsoft.mqtt.pool; public class MqttConnection { private String mqttClient; ; public MqttConnection(String mqttClient) { this.mqttClient = mqttClient; } public String getMqttClient() { return mqttClient; } public void setMqttClient(String mqttClient) { this.mqttClient = mqttClient; } /** * 推送方法消息 */ public void publish(String msg) throws Exception { System.out.println("对象" + mqttClient + ":" + "执行任务" + msg); } @Override public String toString() { return "MqttConnection{" + "id=" + mqttClient + '}'; } }
PoolTest.java
package com.vipsoft.mqtt.pool; import org.apache.commons.pool2.BasePooledObjectFactory; import org.apache.commons.pool2.PooledObject; import org.apache.commons.pool2.impl.DefaultPooledObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.concurrent.atomic.AtomicInteger; public class MqttConnectionFactory extends BasePooledObjectFactory<MqttConnection> { private static final Logger logger = LoggerFactory.getLogger(MqttConnectionFactory.class); // AtomicInteger是一个提供原子操作的Integer类,通过线程安全的方式操作加减 private AtomicInteger counter = new AtomicInteger(); /** * 在对象池中创建对象 * * @return * @throws Exception */ @Override public MqttConnection create() throws Exception { // 实现线程安全避免在高并发的场景下出现clientId重复导致无法创建连接的情况 int count = this.counter.addAndGet(1); MqttConnection mqttConnection = new MqttConnection("MqttConnection:" + count); logger.info("在对象池中创建对象 {}", mqttConnection.toString()); return mqttConnection; } /** * common-pool2 中创建了 DefaultPooledObject 对象对对象池中对象进行的包装。 * 将我们自定义的对象放置到这个包装中,工具会统计对象的状态、创建时间、更新时间、返回时间、出借时间、使用时间等等信息进行统计 * * @param mqttConnection * @return */ @Override public PooledObject<MqttConnection> wrap(MqttConnection mqttConnection) { logger.info("封装默认返回类型 {}", mqttConnection.toString()); return new DefaultPooledObject<>(mqttConnection); } /** * 销毁对象 * * @param p 对象池 * @throws Exception 异常 */ @Override public void destroyObject(PooledObject<MqttConnection> p) throws Exception { logger.info("销毁对象 {}", p.getObject().getMqttClient()); super.destroyObject(p); } /** * 校验对象是否可用 * * @param p 对象池 * @return 对象是否可用结果,boolean */ @Override public boolean validateObject(PooledObject<MqttConnection> p) { logger.info("校验对象是否可用 {}", p.getObject().getMqttClient()); return super.validateObject(p); } /** * 激活钝化的对象系列操作 * * @param p 对象池 * @throws Exception 异常信息 */ @Override public void activateObject(PooledObject<MqttConnection> p) throws Exception { logger.info("激活钝化的对象 {}", p.getObject().getMqttClient()); super.activateObject(p); } /** * 钝化未使用的对象 * * @param p 对象池 * @throws Exception 异常信息 */ @Override public void passivateObject(PooledObject<MqttConnection> p) throws Exception { logger.info("钝化未使用的对象 {}", p.getObject().getMqttClient()); super.passivateObject(p); } }
Atas ialah kandungan terperinci Cara menggunakan teknologi pengumpulan objek Java BasePooledObjectFactory. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!