B4J Library jShellQueue

jmon

Well-Known Member
Licensed User
Hello,

jShellQueue is a queue system for jShell. It is useful in these cases:
  • Set the maximum number of shell commands running at the same time
  • Useful when you need to run lots of commands but don't want to flood your JVM memory.
  • Set a tag to your commands, and retrieve the tag when the command is executed.
  • Get notified when a single job has completed or when the whole queue is finished.
Depends on: jShell (not needed if you load the library)

With this example, I scan all the IP addresses of a subnetwork, but limit the number of concurrent jobs to 25 with timeout of 5 second for each command. You can try to set the concurrent job limit to 0 for unlimited threads:
B4X:
Sub Something
    'Init the Shell queue
    Dim Queue As ShellQueue
    Dim ConcurrentJobs As Int = 25
    Queue.Initialize(ConcurrentJobs, 5000, Me, "mtc")
    For i = 0 To 255
    
        Queue.AddToQueue("cmd.exe", _
            Array("/c", "ping", $"192.168.0.${i}"$, "-n", "4"), _
            "C:/Windows/System32/", _
            False, _
            $"192.168.0.${i}"$)
    
    Next
    Queue.StartQueue
 
End Sub

Sub Queue_JobCompleted(Job As ShellQueueJob)
    Dim Offline As Boolean = Job.StdOut.ToLowerCase.Contains("request timed out") Or Job.StdOut.ToLowerCase.Contains("unreachable")
    If Not(Offline) Then
        Log($"${Job.tag} : Online"$)
    Else
        Log($"${Job.tag} : OFFLINE / UNREACHABLE"$)
    End If    
End Sub

Sub Queue_QueueFinished
    Log($"Queue Finished"$)
End Sub
Note: I recommend to declare your jShellQueue in the sub_globals, and this way have a global queue for your project.

I have attached the library, and the example. The source code is included in the example.

Enjoy
Jmon.
 

Attachments

Last edited:

jmon

Well-Known Member
Licensed User
[TIP]
For the previous example, of scanning the Ip Range, I found that you can set the number of concurrent tasks to unlimited, your computer won't suffer from that. But I was trying to generate thumbnails for thousands of images, and I set the limit to 100 and my computer was running extremely slow.

My tip is to use jAWRobot and set the max number of concurrent tasks to the number of processors, so that it's not too heavy (in my case 12).
 
Top