您好,登錄后才能下訂單哦!
本篇內容主要講解“Java BasePooledObjectFactory對象池化技術如何使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Java BasePooledObjectFactory對象池化技術如何使用”吧!
Java GenericObjectPool 對象池化技術--SpringBoot sftp 連接池工具類
一個對象池包含一組已經初始化過且可以使用的對象,而可以在有需求時創建和銷毀對象。池的用戶可以從池子中取得對象,對其進行操作處理,并在不需要時歸還給池子而非直接銷毀它。這是一種特殊的工廠對象。
BasePooledObjectFactory 對象池化技術的使用
Pom.xml
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.7.0</version> </dependency>
MqttConnection.java
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 + '}'; } }
MqttConnectionFactory.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); } }
PoolTest.java
package com.vipsoft.mqtt; import cn.hutool.core.date.DateUtil; import com.vipsoft.mqtt.pool.MqttConnection; import com.vipsoft.mqtt.pool.MqttConnectionFactory; import org.apache.commons.pool2.impl.GenericObjectPool; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; @SpringBootTest public class PoolTest { @Test void basePooledTest() throws InterruptedException { AtomicInteger atomicInteger = new AtomicInteger(); int excutorCount = 15; CountDownLatch countDownLatch = new CountDownLatch(excutorCount); // =====================創建線程池===================== ExecutorService excutor = Executors.newFixedThreadPool(5); // =====================創建對象池 項目中使用了InitializingBean.afterPropertiesSet() 中創建===================== // 對象池工廠 MqttConnectionFactory personPoolFactory = new MqttConnectionFactory(); // 對象池配置 GenericObjectPoolConfig<MqttConnection> objectPoolConfig = new GenericObjectPoolConfig<>(); objectPoolConfig.setMaxTotal(50); // 對象池 GenericObjectPool<MqttConnection> mqttPool = new GenericObjectPool<>(personPoolFactory, objectPoolConfig); // =====================測試對象池===================== // 循環100次,從線程池中取多個多線程執行任務,來測試對象池 for (int i = 0; i < excutorCount; i++) { excutor.submit(new Thread(() -> { // 模擬從對象池取出對象,執行任務 MqttConnection mqtt = null; try { // 從對象池取出對象 mqtt = mqttPool.borrowObject(); // 讓對象工作 int count = atomicInteger.addAndGet(1); mqtt.publish("Id:" + count + " Time: " + DateUtil.now()); } catch (Exception e) { e.printStackTrace(); } finally { // 回收對象到對象池 if (mqtt != null) { mqttPool.returnObject(mqtt); } countDownLatch.countDown(); } })); } countDownLatch.await(); } }
到此,相信大家對“Java BasePooledObjectFactory對象池化技術如何使用”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。