Engine Hook Example

Last published : Jul 27, 2026
The following example initializes a JDBC database.
package examples.hook;
import com.datasynapse.gridserver.engine.*;
import java.sql.*;
import java.util.*;
/*
 * This is an example of a hook that initializes data from a database.
 * The property "initialized" can be used to discriminate on Services, so that
 * only Engines that have initialized the data will take tasks.
 */
public class JDBCHook extends EngineHook {
    public void initialized() {
        initializeData();
        EngineSession.setProperty("initialized", "true");
    }
    // static method is used by the task
    public static Vector getData() {
        return vData;
    }
 private void initializeData() throws ClassNotFoundException {
        System.out.println("initializing");
        Class cl = Class.forName(getDriver());
        System.out.println("Driver class:" + cl);
 boolean successful = false;
        do {
        try {
                Connection conn = DriverManager.getConnection(getUrl(),
                        getUsername(), getPassword());
                PreparedStatement ps = conn.prepareStatement("select * from
                    people");
                ResultSet rs = ps.executeQuery();
                System.out.println("rs:" + rs);
                while ( rs.next() )
                    vData.add( rowToLine(rs) );
                successful = true;
            } catch (SQLException e) {
                System.out.println("JDBCHook: failed to retrieve data, will try
                                    again.");
            }
            if (!successful) {
                try { Thread.sleep(getFrequency()); } catch (InterruptedException ie) { break; }
            }
} while (!successful);
    }
static String rowToLine( ResultSet input ) throws SQLException {
        StringBuffer buf = new StringBuffer();
        int cols = input.getMetaData().getColumnCount();
        for ( int i=1; i <= cols; i++ ) {
            buf.append(input.getString(i));
            buf.append(' ');
        }
 buf.append('\n');
        return buf.toString();
    }
    public final void setUrl(String url) {
        _url = url;
    }
    public final String getUrl() {
        return _url;
    }
    public final String getDriver() {
        return _driver;
    }
public final void setDriver(String driver) {
        _driver = driver;
    }
 public final String getUsername() {
        return _user;
    }
    public final void setUsername(String user) {
        _user = user;
    }
    public final String getPassword() {
        return _pass;
    }
 public final void setPassword(String password) {
        _pass = password;
    }
    public final void setFrequency(long frequency) {
        _frequency = frequency;
    }
    public final long getFrequency() {
        return _frequency;
    }
    private String _url;
    private String _driver;
    private String _user;
    private String _pass;
    private long _frequency = 5000;
    private static Vector vData = new Vector();
}
The following is also an example of the XML to add to the hooks.xml file for the JDBC example given above.
<hook class="examples.hook.JDBCHook">
        <property name="username" value="sa"/>
        <property name="password" value=""/>
        <property name="url"
            value="jdbc:HypersonicSQL:hsql://%server%:2034"/>
        <property name="driver" value="org.hsql.jdbcDriver"/>
</hook>
Related information