1

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?

Shawn
  • 513
  • 9
  • 18
  • 1
    Hum, there's two things here: on one side you have a DataSource exposed through JNDI (i.e. DB access), on the other side, access to AS400 objects through "jtopen" API (i.e. system objects access), that's two different things –  Jan 19 '17 at 19:57
  • So the answer is no (AFAIK) –  Jan 19 '17 at 19:58
  • They're both using the jt400 driver, just at the moment through two different ways. – Shawn Jan 19 '17 at 20:01
  • It looks to me it should be possible to register an object in JNDI that contains the necessary information to create such a queue object ofr you (just like is done with that `DataSource`); it is just that there is (probably) no standard way to configure this. – Mark Rotteveel Jan 19 '17 at 20:19
  • I agree with your thinking,@MarkRotteveel. I tried to dismantle the Context that came back after looking that up, but it looks like I'm SOL when trying to grab the password from that resource. – Shawn Jan 19 '17 at 20:29
  • I'm not sure it's a good idea to "store" an As400 object. What I meant by "no" is you that you can not use the Datasource to access the dataqueue (there's also some chance your sql user doesn't have access to the dataqueue file) –  Jan 20 '17 at 07:04
  • 2
    I think I understand now. The data source I work with is a massive DB2 instance running on AS400. I mistook a JNDI connection string as a generic connection string I could use even in the instance of non-JDBC. Looks like I'll have to resort to some sort of connection strings in a properties file. – Shawn Jan 20 '17 at 15:14

1 Answers1

0

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).

Shawn
  • 513
  • 9
  • 18