0

我正在开发基于 jax-rs 的 REST 服务。我的应用程序需要连接池,所以我将使用 commons pool2。我计划在实现 ServletContextListener 的类中初始化我的池,以便在应用程序启动时初始化池。问题是我不知道现在如何从我的 FolderService 类中访问我的池。

我试图在下面添加适当的代码。谢谢!

package rest.pool
@WebListener
public class TestPoolListener implements ServletContextListener {

private GenericObjectPool<GenericPool> connectionPool = null;

@Override
public void contextInitialized(ServletContextEvent arg0) {

    final GenericPool[] active = new GenericPool[10];

    PoolFactory poolFactory = new PoolFactory();

    connectionPool = new GenericObjectPool<GenericPool>(poolFactory,config);

    try {

        for(int i=0 ; i<5 ; i++) {
            active[i] = connectionPool.borrowObject();
        }

        for(int i=0 ; i<5 ; i++) {
           connectionPool.returnObject(active[i]);
        }


    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public class MyPool extends BasePooledObjectFactory<ODServer>{
        @Override
        public ODServer create() {
            //create code here
        }

   }

package rest.resource;

import javax.ws.rs.*;

@Path("/folders")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)

public class FolderResource {

    FolderService folderService = new FolderService();

    @GET
    public List<Folder> getFolders(@Context UriInfo uriInfo) {

        return folderService.getAllFolders();

    }

}

package rest.service;

public class FolderService {

    public List<Folder> getAllFolders(String uri) {
        List<Folder> folders = new ArrayList<>();
        **//How do I get access to the connection pool from here?**
        return folders ;
    }

}
4

0 回答 0