Search code examples
javajdbcjndiibm-midrangejt400

AS400 system connection with JNDI


I can figure out how to connect to an AS400 through jt400 with JNDI resources just fine:

    Connection conn = null;
    Statement stmt = null;
    try {
        Context ctx = (Context) new InitialContext().lookup("java:comp/env");
        conn = ((DataSource) ctx.lookup("jdbc/AS400")).getConnection();
        System.out.println(conn.getClientInfo());

        stmt = conn.createStatement();
        //SQL data fetch using the connection
        ResultSet rs = stmt.executeQuery("SELECT * FROM LIBRARY.TABLE");
        while (rs.next()) {
            System.out.println(rs.getString("COLUMN1"));
        }
        conn.close();
        conn = null;
    }
    catch(Exception e){System.out.println(e);}

However, another part of the application utilizes DataQueues (from the same jt400 library):

    String queue = "/QSYS.LIB/" + libraryName +".LIB/" + queueName +".DTAQ";

    try{
        system = new AS400(server, user, pass);
        DataQueue dq = new DataQueue(system, queue);

        // Convert the Data Strings to IBM format
        byte[] byteData = message.getBytes("IBM285");

        dq.write(byteData);
        System.out.println("Wrote to DataQueue");

    }catch(Exception e){
        e.printStackTrace();
        System.err.println(e);
    }finally{
        // Make sure to disconnect
        if(system != null){
            try{
                system.disconnectAllServices();
                System.out.println("Disconnected from DataQueue.");
            }catch(Exception e){
                System.err.println(e);
            }
        }
    }

Inside of this working code for DataQueues references server, user, pass, which isn't ideal.

I'd like to utilize the AS400 JNDI connection I already set up, but every example I see about connecting Java to DataQueues references an example much like this one.

The documentation all seem to point to AS400 system objects which are hard-coded references to servername, user, pass, etc.

Is there better way to utilize DataQueue() with a JNDI reference?


Solution

  • As assumed in the comments above, the DataQueue is not part of the JDBC connection at all, it can't be used to configure the connection for usage to reading and writing to a DataQueue. Since this is the case, it can't also share connection methods that JDBC uses even though the jt400 library connects with JDBC. A properties file or other server-based solutions is required unless a hard-coded connection is specified in the DataQueue/Java examples online (All 1 of them).