Skip to main content
Skip table of contents

Swiftlet API

Since the 12.0.0 release, the Swiftlet API documentation has been phased out, as the preference shifted towards using SwiftMQ Streams or Flow Director Platform for plugin functionalities.

For those with existing custom Swiftlets who wish to transition to SwiftMQ 13, modifications will be necessary to align with the new features of the Threadpool Swiftlet. This update requires adapting the custom Swiftlets code to function correctly within the enhanced SwiftMQ 13 environment.

Using Event Loops

For scenarios involving a single-threaded context that operates in an event-driven manner, an Event Loop from the Threadpool Swiftlet could be an ideal choice. This is particularly suitable for scenarios where efficient event handling and processing are crucial, and where maintaining a consistent thread for event handling can optimize performance and resource management

Create an Event Loop:

JAVA
ThreadpoolSwiftlet threadpoolSwiftlet = (ThreadpoolSwiftlet) SwiftletManager.getInstance().getSwiftlet("sys$threadpool");
EventLoop eventLoop = threadpoolSwiftlet.createEventLoop("myswiftlet.processor", 
                                                          list -> list.forEach(e -> /* do something */);

When configuring an Event Loop for a single-threaded, event-driven context, you can identify and manage it using a unique identifier, such as myswiftlet.processor. This identifier is used to add and configure the Event Loop within the Threadpool Swiftlet configuration, specifically under the group swiftlet section. This allows for precise and effective management of the Event Loop, tailored to the specific needs of your Swiftlet.

XML
      <group name="swiftlet">
        <eventloops>
          <eventloop name="sys$mgmt.dispatch"/>
          <eventloop name="sys$topicmanager.announce"/>
          <eventloop name="sys$scheduler.scheduler"/>        
          <eventloop name="myswiftlet.processor"/>
        </eventloops>
      </group>

The second parameter to createEventLoop needs to implement the EventProcessor interface:

JAVA
package com.swiftmq.swiftlet.threadpool;

import java.util.List;

@FunctionalInterface
public interface EventProcessor {
    void process(List<Object> events);
}

Submit events:

To submit events to the loop for processing, use:

JAVA
eventLoop.submit(event);

Close an Event Loop:

If you don’t need the Event Loop anymore, you should close it:

JAVA
eventLoop.close();

Running Asynchronous Tasks

To execute on-demand asynchronous tasks in SwiftMQ, you can use the runAsync method provided by the Threadpool Swiftlet. This method allows you to handle tasks asynchronously, ensuring that they are executed efficiently without blocking the main thread. The runAsync method is particularly useful for tasks that need to be run separately from the main execution flow, allowing for smoother operation and better resource management in your SwiftMQ environment.

JAVA
CompletableFuture<?> future = threadpoolSwiftlet.runAsync(<Runnable>);

By default, the runAsync method in SwiftMQ's Threadpool Swiftlet executes the provided <Runnable> on a Virtual Thread. However, if you need to run your task on a platform thread instead, SwiftMQ provides an alternative method or option within runAsync to specify this preference.

JAVA
CompletableFuture<?> future = threadpoolSwiftlet.runAsync(<Runnable>, false);

When using the runAsync method in SwiftMQ's Threadpool Swiftlet to run tasks, it's important to note that you cannot pass data back from the Runnable via the future. This limitation means that while the task can be executed asynchronously, any data it generates will need to be handled or communicated back through other means, not through the future object associated with the runAsync call.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.