DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Throttling in Spring Integration

Throttling in Spring Integration

Follow along with this tutorial to learn how to set up throttling in your system using a few different open source frameworks along with Java and XML code.

Upender Chinthala user avatar by
Upender Chinthala
·
Feb. 01, 18 · Tutorial
Like (5)
Save
Tweet
Share
27.62K Views

Join the DZone community and get the full member experience.

Join For Free

Writing Throttlers in Spring Integration is very simple. In my current project, I was asked to send 15 SOAP requests per second to a downline system and not to send more than that; and to make sure that it was parallel. For more information or to understand Spring Integration, follow this link to the docs. In the below diagram, the left side Producer threads are publishing messages to the Spring Integration Executor channel type, and this Executor channel bridges to another channel called "Bridge Channel," which is a queue Channel type and keeps messages from the Executor Channel. The queue channel has Poller and it pulls a maximum of 15 messages per second. On the other side of the Service Activator, the endpoint receives 15 messages per second and adds additional information to messages before publishing them to another Executor channel (where this Executor Channel has subscribed Service Activator as a worker thread). I have written some sample code using Spring Integration JUnit with XML configuration. Image title

1.  Define the input channel  - Spring Integration Executor Channel. 

<int:channel id="inputChannel">
   <int:dispatcher task-executor="producerThreadExecutor"/>
</int:channel>

<task:executor id="producerThreadExecutor" pool-size="50" queue-capacity="10"  rejection-policy="DISCARD" />

2. Spring JUnit test. This class publishes a message to the inputChannel using Java multi-threads.

package com.uppi.poc.throttler;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.channel.ExecutorChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:application-test.xml")
public class SpringThrottlerTest {

 @Autowired
 private ExecutorChannel inputChannel;
 private AtomicInteger value = new AtomicInteger(1);

 @Test
 public void showMe() throws Exception {
  ThreadPoolExecutor be = new ThreadPoolExecutor(100, 200, 1000, TimeUnit.MILLISECONDS,
   new LinkedBlockingQueue < Runnable > ());
  for (int i = 1; i <= 90; i++) {
   be.submit(new TaskSubmiter(inputChannel, value));
  }
  TimeUnit.SECONDS.sleep(28);
 }

 private static class TaskSubmiter implements Runnable {
  private final ExecutorChannel inputChannel;
  private final AtomicInteger i;

  public TaskSubmiter(final ExecutorChannel testChannel, final AtomicInteger i) {
   this.inputChannel = testChannel;
   this.i = i;
  }

  @Override
  public void run() {
   inputChannel.send(
    MessageBuilder.withPayload(Thread.currentThread().getName() + ",= " + i.getAndIncrement()).build());
  }

 }
}

3. Define QueueChannel with a capacity of 100.

<int:channel id="throttlerChannel">
       <int:queue capacity="100"/> <!-- for example queue size, you can increase this capacity based on your requirement -->
 </int:channel>

4. This step can be optional. If you don't have any additional information to add to the message, just use another bridge with a poller. See below: 

<int:bridge input-channel="inputChannel" output-channel="throttlerChannel" />

<int:service-activator  ref="producer" method="process" input-channel="throttlerChannel">
   <int:poller fixed-delay="1000" max-messages-per-poll="15"></int:poller> <!-- per second 15 messages -->
</int:service-activator>

<bean id="producer" class="com.uppi.poc.throttler.MessageChunkProducer" />

OR

Bridge from the Queue Channel to the Executor Channel (use this bridge only if you don't have any dependencies or additional information to add to the message).

<int:bridge input-channel="throttlerChannel" output-channel="receiverChannel" >
     <int:poller fixed-delay="1000" max-messages-per-poll="15"></int:poller>
</int:bridge>

<!-- comment the following code -->
<!--  <int:service-activator  ref="producer" method="process" input-channel="throttlerChannel">
        <int:poller fixed-delay="1000" max-messages-per-poll="3"></int:poller>
</int:service-activator>
 <bean id="producer" class="com.uppi.poc.throttler.MessageChunkProducer" /> -->

5. Publishing a message to the Executor channel where a worker thread consumes it (optional, see step 4).

package com.uppi.poc.throttler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.channel.ExecutorChannel;
import org.springframework.integration.support.MessageBuilder;

public class MessageChunkProducer {

 @Autowired
 private ExecutorChannel receiverChannel;
 public void process(String msg) {
  receiverChannel.send(MessageBuilder.withPayload(msg).build());
 }
}

6.  Define the Receiver Executor Channel, where the worker thread can consume it.

<int:channel id="receiverChannel">
       <int:dispatcher task-executor="workerThreadExecutor"/>
</int:channel>
<task:executor id="workerThreadExecutor" pool-size="15" queue-capacity="10"  rejection-policy="DISCARD" />

<int:service-activator id="worker" ref="workerThread" method="process" input-channel="receiverChannel" />
<bean id="workerThread" class="com.uppi.poc.throttler.WorkerThread" /> 

7. Define the Service Activator endpoint (worker thread).

package com.uppi.poc.throttler;

import java.util.Date;
import java.util.logging.Logger;

public class WorkerThread {
 private static final Logger LOG = Logger.getLogger(WorkerThread.class.getName());
 public void process(String msg) {
  LOG.info("Date " + new Date() + ",Thread " + Thread.currentThread().getName() + ", Msg= " + msg);
 }
}

The complete code for this article can be accessed on GitHub.

Sample Output:

Image title

Spring Integration Spring Framework Integration

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Three SQL Keywords in QuestDB for Finding Missing Data
  • Using QuestDB to Collect Infrastructure Metrics
  • Handling Virtual Threads
  • A Beginner's Guide to Back-End Development

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: