文件夹侦听器过早终止

时间:2012-05-10 18:03:49

标签: java mysql listener directory

此问题与以下帖子有关:Previous article

使用Jigar提供的代码,但由于某种原因,程序会在几秒后停止。在调试过程中,我意识到它永远不会超越这条线:

int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());

知道可能导致这种情况的原因吗?

不过,我已经对它进行了一些修改。它可能与它有关,我只是不知道:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Calendar;

import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;

/**
 *
 * @author Jigar
 */
public class FolderListener {

    public void sample() throws Exception {
        // path to watch
        String path = System.getProperty("C:\\Program Files\\Apache Software Foundation\\Tomcat 7.0\\webapps\\ITPDP\\JSP\\Pictures");

        // watch mask, specify events you care about,
        // or JNotify.FILE_ANY for all events.
        int mask = JNotify.FILE_CREATED
                | JNotify.FILE_DELETED
                | JNotify.FILE_MODIFIED
                | JNotify.FILE_RENAMED;

        // watch subtree?
        boolean watchSubtree = true;
        System.out.println("1...");
        // add actual watch
        int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());

        // sleep a little, the application will exit if you
        // don't (watching is asynchronous), depending on your
        // application, this may not be required
        System.out.println("2...");
        Thread.sleep(1000000);
        System.out.println("2½...");
        // to remove watch the watch
        boolean res = JNotify.removeWatch(watchID);
        if (!res) {
            // invalid watch ID specified.
        }
    }

    class Listener implements JNotifyListener {

        @Override
        public void fileRenamed(int wd, String rootPath, String oldName, String newName)
        {
        }

        @Override
        public void fileModified(int wd, String rootPath, String name)
        {
        }

        @Override
        public void fileDeleted(int wd, String rootPath, String name)
        {
        }

        @Override
        public void fileCreated(int wd, String rootPath, String name)
        {
            try
            {
                System.out.println("3...");
                String connectionURL = "jdbc:mysql://[our SQL URL]";
                String myDriver = "com.mysql.jdbc.Driver";
                Class.forName(myDriver).newInstance();
                Connection con = DriverManager.getConnection(connectionURL);

                String query = "INSERT INTO Conversations (ConversationID, Content1, Content2, Date) VALUES (587, '', 'http://localhost:8080/ITPDP/JSP/Pictures/" + name + ".jpg', " + Calendar.getInstance().getTimeInMillis() + ")";

                //Create a Statement class to execute the SQL statement
                PreparedStatement stmt = con.prepareStatement(query);

                stmt.executeUpdate();
                System.out.println("4...");

                con.close();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }


    }
    public static void main(String[] args) throws Exception {
        System.out.println("0...");
//        System.out.println(System.getProperties());
        new FolderListener().sample();
    }
}

0 个答案:

没有答案