1

我为记录用户活动的程序编写了一个插件。如果用户按下按钮并因此触发动作,则应将一些内容写入数据库。这个过程应该并行运行,因为它不应该延迟触发的动作:

if (con != null) {
        SmartClientKernel.addPooledTask(new Runnable() {
            @Override
            public void run() {
                ActionDispatcher.getInstance().addDispatchExtension(new IDispatchExtension() {
                    @Override
                    public void preprocessActionExecution(RPAction action) {
                        Calendar cal = Calendar.getInstance();
                        Date startTime = cal.getTime();
                        String actionName = action.getName();
                        java.sql.Timestamp sqlTime = new java.sql.Timestamp(startTime.getTime());
                        UUID id = UUID.randomUUID();
                        PreparedStatement statement;
                        try {
                            statement = con.prepareStatement("INSERT INTO " + tableName
                                    + " (ID, USERNAME, SESSIONID, PROJECTNAME, STARTTIME, ACTIONTYPE, ACTIONNAME) VALUES (?,?,?,?,?,?,?)");
                            statement.setString(1, id.toString());
                            statement.setString(2, userName);
                            statement.setString(3, sessionId);
                            statement.setString(4, projectName);
                            statement.setTimestamp(5, sqlTime);
                            statement.setString(6, "Action");
                            statement.setString(7, actionName);
                            statement.execute();
                            statement.close();
                        } catch (SQLException e) {
                            Log.getLogger().log(Level.SEVERE, e.getMessage(), e);
                        }

                    }
                });
            }
        });
    }

实际上,它工作得很好,但我想知道编程风格是否可以这样,因为我可以移动块:

SmartClientKernel.addPooledTask(new Runnable() {
        @Override
        public void run() {

进入方法 preprocessActionExecution() 似乎也有效。

你们有什么感想?

更新: 谢谢你们的反馈!

4

2 回答 2

3

我一直觉得,如果一段代码可以有自己的名字,那么它应该有自己的名字

或者,换一种说法,如果它看起来像一个类并且行为像一个类,那么它应该是一个类

所以 - 回答你的问题 - 在我看来,这些匿名内部类中的每一个都应该是不同的类。虽然它们不需要是公共课程,但它们作为static内部人员就可以了,但它们应该被分解并有一个名字。

另外——你准备好的陈述应该在别处准备。根据使用情况对其进行准备是浪费时间。

于 2013-05-17T08:49:11.397 回答
0

如果你不喜欢你这样做的方式,这是另一种方式:

anyClassMethod() {
    if (con != null) {
        SmartClientKernel.addPooledTask(new MyRunnable(con, tableName, userName, sessionId, projectName));
    }
}

private class MyRunnable implements Runnable {

    Connection con;
    String tableName, userName, sessionId, projectName;

    MyRunnable(Connection con, String tableName, String userName, String sessionId, String projectName) {
        this.con = con;
        this.tableName = tableName;
        this.userName = userName;
        this.sessionId = sessionId;
        this.projectName = projectName;
    }

    @Override
    public void run() {
        ActionDispatcher.getInstance().addDispatchExtension(new MyIDispatchExtension(con, tableName, userName, sessionId, projectName));
    }
}

private class MyIDispatchExtension (extends or implements) IDispatchExtension {

    Connection con;
    String tableName, userName, sessionId, projectName;

    public MyIDispatchExtension(Connection con, String tableName, String userName, String sessionId, String projectName) {
        this.con = con;
        this.tableName = tableName;
        this.userName = userName;
        this.sessionId = sessionId;
        this.projectName = projectName;
    }

    @Override
    public void preprocessActionExecution(RPAction action) {
        Calendar cal = Calendar.getInstance();
        Date startTime = cal.getTime();
        String actionName = action.getName();
        java.sql.Timestamp sqlTime = new java.sql.Timestamp(startTime.getTime());
        UUID id = UUID.randomUUID();
        PreparedStatement statement;
        try {
            statement = con.prepareStatement("INSERT INTO " + tableName
                    + " (ID, USERNAME, SESSIONID, PROJECTNAME, STARTTIME, ACTIONTYPE, ACTIONNAME) VALUES (?,?,?,?,?,?,?)");
            statement.setString(1, id.toString());
            statement.setString(2, userName);
            statement.setString(3, sessionId);
            statement.setString(4, projectName);
            statement.setTimestamp(5, sqlTime);
            statement.setString(6, "Action");
            statement.setString(7, actionName);
            statement.execute();
            statement.close();
        } catch (SQLException e) {
            Log.getLogger().log(Level.SEVERE, e.getMessage(), e);
        }

    }
}
于 2013-05-17T09:20:18.820 回答