Tuesday, March 12, 2013

Service Throttling

We had to throttle the end service, and following are the two approaches which came quite handy:  

1. Out of the Box OSB
For the Business Service in OSB, we can configure the throttling as below. It allows us to configure concurrent threads, thread queue size, message expiration

  
2. HTTP Proxy Service
There could be a scenario where we don't have OSB or we don't want to use OSB to avoid piling up the request affect other important integration. The other standard approach to achieve similar thing via HTTP Proxy servlet and throttle down the number of threads to desired value and queue rest of the requests. This is involves some coding but it can be deployed on any weblogic managed server.

Create a Proxy Servlet
Instead of writing code from scratch, used com.jsos.httpproxy.HttpProxyServlet as below:

<?xml version = '1.0' encoding = 'windows-1252'?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
  <servlet>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>com.spring.service.proxy.TestServlet</servlet-class>
  </servlet>
  
  <servlet>
    <servlet-name>ProxyServlet</servlet-name>
    <servlet-class>com.jsos.httpproxy.HttpProxyServlet</servlet-class>
    <init-param>
      <param-name>host</param-name>
      <param-value>http://localhost:7001/NotificationService/NotificationPort</param-value>
    </init-param>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/testservlet</url-pattern>
  </servlet-mapping>
  
  <servlet-mapping>
    <servlet-name>ProxyServlet</servlet-name>
    <url-pattern>/proxyservlet</url-pattern>
  </servlet-mapping>
  
</web-app>


Configure thread throttling
We can do that using following two different ways:

a) Local Work Manager in weblogic.xml
Configure Work Manager and Servlet dispatch policy in weblogic.xml

weblogic.xml
<?xml version = '1.0' encoding = 'windows-1252'?>
<weblogic-web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-web-app http://www.bea.com/ns/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd" xmlns="http://www.bea.com/ns/weblogic/weblogic-web-app">
  
  <wl-dispatch-policy>LocalProxyWorkManager</wl-dispatch-policy>                  
  
  <servlet-descriptor>
    <servlet-name>ProxyServlet</servlet-name>
    <dispatch-policy>LocalProxyWorkManager</dispatch-policy>
  </servlet-descriptor>
  
  <work-manager>
    <name>LocalProxyWorkManager</name>
    <max-threads-constraint>
      <name>LocalProxyWorkManagerMaximumThreadConstraint</name>
      <count>1</count>
    </max-threads-constraint>
    <capacity>
      <name>LocalProxyWorkManagerCapacityConstraint</name>
      <count>1000</count>
    </capacity>
    <ignore-stuck-threads>true</ignore-stuck-threads>
  </work-manager>
  
</weblogic-web-app>


b) Global Work Manager in weblogic.xml and Weblogic Console 
Configure Work Manager in Weblogic Console and Servlet dispatch policy in weblogic.xml

weblogic.xml
<?xml version = '1.0' encoding = 'windows-1252'?>
<weblogic-web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-web-app http://www.bea.com/ns/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd" xmlns="http://www.bea.com/ns/weblogic/weblogic-web-app">
  
  <wl-dispatch-policy>GlobalWorkManager</wl-dispatch-policy>                  
  
  <servlet-descriptor>
    <servlet-name>ProxyServlet</servlet-name>
    <dispatch-policy>GlobalWorkManager</dispatch-policy>
  </servlet-descriptor>
  
</weblogic-web-app>


Weblogic Console:




Source code can be downloaded from here.

2 comments:

Randy said...

Hi Chintan,
Just wondering why you need the HTTP Proxy servlet to map to a WebLogic Work Manager instead of just configuring the Work Manager for the TestServlet in your example.

Chintan Shah said...

Hello Randy,
Not sure if I understand you correctly, but I think both are valid options. We can just use local work manager and put in weblogic.xml.