Search code examples
ibm-midrangejobsjt400

JT400 - Replying to MSGW Job


Is it possible to reply to a MSGW job in AS400 from JT400?
I've got the Job element and I can know if it's in MSGW status by Job.MESSAGE_REPLY_WAITING

Ex: normally I use "C" via WRKACTJOB


Solution

  • This is the code that works. I think it can be shortened and optimized.
    There must be a better way!

    public boolean answer(String answer) throws MyOperationException {
       if (answer == null || answer.length() > 1) {
          throw new MyOperationException();
       }
    
       MessageQueue msgq = new MessageQueue(as.getAS400(), QSYSObjectPathName.toPath(MyAS400.LIBRARY_LIST, "QSYSOPR", "MSGQ"));
       msgq.setSelectMessagesNeedReply(true);
       msgq.setListDirection(false);
    
       try {
          Enumeration m = msgq.getMessages();
    
          while (m.hasMoreElements()) {
             QueuedMessage msg = (QueuedMessage) m.nextElement();
    
             if (msg.getFromJobNumber().trim().equals(getNumber())) {
                msgq.reply(msg.getKey(), answer);
    
                return true;
             }
          }
       } catch (AS400SecurityException | ErrorCompletingRequestException | InterruptedException | IOException | ObjectDoesNotExistException ex) {
          ex.printStackTrace();
       }
    
       return false;
    }
    

    If you don't know the message queue, you can use ObjectList.