All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.camel.component.netty.SharedSingletonObjectPool Maven / Gradle / Ivy

There is a newer version: 4.9.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.camel.component.netty;

import java.util.NoSuchElementException;

import org.apache.commons.pool2.ObjectPool;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * An {@link org.apache.commons.pool2.ObjectPool} that uses a single shared instance.
 * 

* This implementation will always return 1 in {@link #getNumActive()} and return 0 in * {@link #getNumIdle()}. */ public class SharedSingletonObjectPool implements ObjectPool { private static final Logger LOG = LoggerFactory.getLogger(SharedSingletonObjectPool.class); private final PooledObjectFactory factory; private volatile PooledObject t; public SharedSingletonObjectPool(PooledObjectFactory factory) { this.factory = factory; } @Override public void addObject() throws Exception, IllegalStateException, UnsupportedOperationException { // noop } @Override public synchronized T borrowObject() throws Exception, NoSuchElementException, IllegalStateException { if (t != null) { // ensure the object is validate before we borrow it if (!factory.validateObject(t)) { invalidateObject(t.getObject()); LOG.info("Recreating new connection as current connection is invalid: {}", t); t = null; } } if (t == null) { t = factory.makeObject(); } return t.getObject(); } @Override public void clear() throws Exception, UnsupportedOperationException { t = null; } @Override public void close() { t = null; } @Override public int getNumActive() { return 1; } @Override public int getNumIdle() { return 0; } @Override public void invalidateObject(T obj) throws Exception { t = null; } @Override public void returnObject(T obj) throws Exception { // noop } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy