我可以改进此代码以获得更好的性能吗?

时间:2016-02-18 17:26:34

标签: java rest jdbc

我在JAVA中使用连接到数据库的泽西的REST WS。我不知道什么是理想的执行时间,但我觉得时间太长了。

对DB的实际调用在0-3毫秒的范围内完成,但完成REST请求的总时间大约为9毫秒。

以下是其中一种方法:

connection // declared as instance variable
preparedStatement //declared as instance variable 
public int insertSubscription(ActiveWatchers activeWatchers) throws SQLException {

        int index = 0;

        try {
            connection = DAOConnectionFactory.getConnection();
            preparedStatement = connection.prepareStatement(INSERT_SUBS);
            preparedStatement.setObject(++index, activeWatchers.getPresentityURI());
            preparedStatement.setObject(++index, activeWatchers.getCallId());
            preparedStatement.setObject(++index, activeWatchers.getToTag());
            preparedStatement.setObject(++index, activeWatchers.getFromTag());
            preparedStatement.setObject(++index, activeWatchers.getToUser());
            preparedStatement.setObject(++index, activeWatchers.getToDomain());
            preparedStatement.setObject(++index, activeWatchers.getWatcherUsername());
            preparedStatement.setObject(++index, activeWatchers.getWatcherDomain());
            preparedStatement.setObject(++index, activeWatchers.getEvent());
            preparedStatement.setObject(++index, activeWatchers.getEventId());
            preparedStatement.setObject(++index, activeWatchers.getLocalCseq());
            preparedStatement.setObject(++index, activeWatchers.getRemoteCseq());
            preparedStatement.setObject(++index, activeWatchers.getExpires());
            preparedStatement.setObject(++index, activeWatchers.getStatus());
            preparedStatement.setObject(++index, activeWatchers.getReason());
            preparedStatement.setObject(++index, activeWatchers.getRecordRoute());
            preparedStatement.setObject(++index, activeWatchers.getContact());
            preparedStatement.setObject(++index, activeWatchers.getLocalContact());
            preparedStatement.setObject(++index, activeWatchers.getVersion());
            preparedStatement.setObject(++index, activeWatchers.getSocketInfo());

            long start = System.currentTimeMillis();
            int status = preparedStatement.executeUpdate();
            long end = System.currentTimeMillis();
            logger.debug("insertSubscription elasped time {}", (end - start));
            logger.debug("Insert returned with status {}.", status);
            return status;

        } catch (SQLException ex) {
            logger.error("Error while adding new subscription by {}@{} for {} into database.", activeWatchers.getWatcherUsername(), activeWatchers.getWatcherDomain(), activeWatchers.getPresentityURI(), ex);
            throw ex;
        } catch (Exception ex) {
            logger.error("Error while adding new subscription by {}@{} for {} into database.", activeWatchers.getWatcherUsername(), activeWatchers.getWatcherDomain(), activeWatchers.getPresentityURI(), ex);
            throw ex;
        } finally {
            DAOConnectionFactory.closeConnection(connection, preparedStatement, null);
        }

    }

REST部分

subscriptionDAO //declared as instance variable
@POST
    @Consumes("application/json")
    public Response addSubscription(ActiveWatchers activeWatchers) {
        long start = System.currentTimeMillis();
        logger.debug("addSubscription start time {}", start);
        subscriptionDAO = new SubscriptionDAO();
        try {
            subscriptionDAO.insertSubscription(activeWatchers);
            long end = System.currentTimeMillis();
            logger.debug("addSubscription elasped time {}", (end - start));
            return Response.status(201).build();
        } catch (Exception ex) {
            logger.error("Error while creating subscription.", ex);
            return Response.status(500).entity("Server Error").build();
        }
    }

我有很多其他类似的功能用于不同的操作,每个都有类似的行为,这会影响系统的整体性能。

由于

1 个答案:

答案 0 :(得分:1)

  

对DB的实际调用在0-3毫秒的范围内完成,但完成REST请求的总时间大约为9毫秒。

我认为如果您的网络层仅导致6毫秒的开销,那么它非常快。我想6ms主要用于反射密集的JSON反序列化(进入ActiveWatcher实例)。

首先,您应该使用VisualVM(GUI应用程序,JDK的一部分)来分析您的应用程序,因为基于猜测进行优化只是一个蹩脚的事情。

如果事实证明json反序列化是瓶颈,那么你可以为你的ActiveWatchers类开发一个自定义jackson反序列化器,你可以利用手写代码而不是基于慢反射的行为。

但我仍然认为你的9ms足够快。