DEV Community

jreighley
jreighley

Posted on

3 1

Clojure on the IBM i

I have been using Clojure to make API's that talk to IBMi servers.

Clojure is nice because it interops nicely with Java, so it can access the IBMi via the JT400 toolkit.

I do a lot of support for various clients running IBMi servers. There are many troubleshooting exercises that I used to have to do manually, serially and asynchronously. Connect to a client, run a report, scan the results, check a constant here, check a constant there - verify certain jobs are running and have no messages etc.

I can now point my browser at an URL, and my Clojure program will run the whole troubleshooting tree, and present me a webpage showing me all of the results. Often I can even include a "Fix it button" next to the problems.

If you are interested, here is a pretty bare bones starter gist:

~~~~
(ns jtcompojure.ASconn
(:require [clojure.java.jdbc :as j])
(:gen-class)
(:import
(com.ibm.as400.access AS400ConnectionPool
CommandCall)))
;; I make a list of systems I am going to connnect to...
;; I can add keys for anything I would like that might be different amoungst system
(def syslist [{:name "SPOKANE" :iseries true :address "ipaddress" :dftlib "Libary"}]
;; I have a function that finds the system in the list, and appends it's keys to a default set of keys and values.
(defn sysinfo [sysname]
(conj {:user "Default-USER" :pass "Default-PASSWORD" :mdlib "Default-lib"} (first (filter #(= sysname (:name %) )syslist))))
;; make a connection pool
(def pool (AS400ConnectionPool.))
;;Here is a function to run any command.
(defn as400command "Given a system and a command line command, sends the command for execution" [system command]
(let [creds (sysinfo system) ;; fetches the credentials for the system argument
AS400conn (.getConnection pool (:address creds) (:user creds) (:pass creds)) ;;gets a connection from the pool
AS400Comm (CommandCall. AS400conn) ;;creates a Java CommandCall object)
success (.run AS400Comm command) ;; Calls the .run command with the command argument
results (mapv #(str %) (.getMessageList AS400Comm))] ;; gathers the resulting messages
(.returnConnectionToPool pool AS400conn) ;; releases the connection back to the pool
results)) ;returns the results
~~~~
;; trying it from the repl:
(as400command "SPOKANE" "PING '172.16.1.2'")
=>
["AS400Message (ID: TCP3204 text: Verifying connection to host system 172.16.1.2.):com.ibm.as400.access.AS400Message@5975fe7c"
"AS400Message (ID: TCP3215 text: PING reply 1 from 172.16.1.2 took 0 ms. 256 bytes. TTL 64.):com.ibm.as400.access.AS400Message@216c7a48"
"AS400Message (ID: TCP3215 text: PING reply 2 from 172.16.1.2 took 0 ms. 256 bytes. TTL 64.):com.ibm.as400.access.AS400Message@1c713e9e"
"AS400Message (ID: TCP3215 text: PING reply 3 from 172.16.1.2 took 0 ms. 256 bytes. TTL 64.):com.ibm.as400.access.AS400Message@7521c01e"
"AS400Message (ID: TCP3215 text: PING reply 4 from 172.16.1.2 took 0 ms. 256 bytes. TTL 64.):com.ibm.as400.access.AS400Message@35156d8"
"AS400Message (ID: TCP3215 text: PING reply 5 from 172.16.1.2 took 0 ms. 256 bytes. TTL 64.):com.ibm.as400.access.AS400Message@41adc2c0"
"AS400Message (ID: TCP3211 text: Round-trip (in milliseconds) min/avg/max = 0/0/0.):com.ibm.as400.access.AS400Message@543d91a5"
"AS400Message (ID: TCP3210 text: Connection verification statistics: 5 of 5 successful (100 %)
view raw gistfile1.txt hosted with ❤ by GitHub

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay