java - 我可以改进这段代码以获得更好的性能吗?

标签 java rest jdbc

我有一个 JAVA 中的 REST WS,使用连接到数据库的 jersey。我不知道理想的执行时间应该是多少,但我觉得花费的时间太多了。

对数据库的实际调用在 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();
        }
    }

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

谢谢

最佳答案

The actual call to DB completes in range of 0-3 milliseconds but the overall time to complete the REST request takes >9 milliseconds.

我认为如果你的 web 层只造成 6ms 的开销,那么它就相当快了。我猜想 6 毫秒主要花在反射密集型 JSON 反序列化(到 ActiveWatcher 实例中)上。

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

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

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

关于java - 我可以改进这段代码以获得更好的性能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35488360/

相关文章:

php - Azure 使用 REST API 将 Blob 从一个存储帐户复制到同一订阅中的另一个存储帐户

java - 在namedParameterJdbcTemplate参数中使用什么更好

java - AES_ENCRYPT 与 mysql 服务器 5.5

java - JSch sftp 上传/下载进度

node.js - 为 node.js Web 服务应用程序安装服务器

rest - 如何模拟超时属性?

java - 为什么 DateTime 字段上的 getTimestamp 返回的结果与选择 unix_timestamp 的结果不同?

java - Java中如何引用Method对象?

java - 如何通过简单地调用其名称来从终端运行java应用程序?

Java 关于初始化 swing 组件的困惑