Tag Archives: Tomcat

Bug in Tomcat 8.0.12

There are several fixes to make in the documentation of the JDBC-based store.

https://issues.apache.org/bugzilla/show_bug.cgi?id=57147 (vote for it!)

Advertisements

How to Setup JDBC Persistence in Tomcat/TomEE

1) Create the database.

2) Create the table:

CREATE TABLE tomcat$sessions (
  id VARCHAR(100) NOT NULL PRIMARY KEY,
  app VARCHAR(255),
  data MEDIUMBLOB,
  lastaccess BIGINT NOT NULL,
  maxinactive INT NOT NULL,
  valid CHAR(1) NOT NULL,
  KEY kapp(app)
);

3) Change the <Tomcat/TomEE Install Dir>/conf/context.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
  <Manager className="org.apache.catalina.session.PersistentManager">
    <Store className="org.apache.catalina.session.JDBCStore" driverName="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/sample_db" connectionName="username" connectionPassword="password" />
  </Manager>
</Context>

4) Start Tomcat/TomEE.

You can find more details here.

How to Create a Store for Tomcat/TomEE

import org.apache.catalina.Session;
import org.apache.catalina.session.StoreBase;

public final class SampleStore extends StoreBase {

  @Override
  public void clear() {
  }

  @Override
  public int getSize() {
    return 0;
  }

  @Override
  public String[] keys() {
    return new String[] {};
  }

  @Override
  public Session load(final String id) {
    return null;
  }

  @Override
  public void remove(final String id) {
  }

  @Override
  public void save(final Session session) {
  }

}

A sample is available here.

How to Setup Load Balancing in Tomcat/TomEE with mod_proxy_balancer

1) Create the <Apache Install Dir>/conf.d/mod_proxy_balancer.conf file:

LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule status_module modules/mod_status.so

<Location /balancer-manager>
  SetHandler balancer-manager
</Location>

<Proxy balancer://sample-balancer>
  BalancerMember ajp://host1:8009
  BalancerMember ajp://host2:8009
  ProxySet scolonpathdelim=On
  ProxySet stickysession=JSESSIONID
</Proxy>

ProxyPass /sample balancer://sample-balancer

2) Change the <Tomcat/TomEE Install Dir>/conf/server.xml file in each node:

<?xml version="1.0" encoding="UTF-8"?>
<Server>
  ...
  <Service>
    ...
    <Engine jvmRoute="host1">
      ...
    </Engine>
  </Service>
</Server>

3) Start Apache.

4) Start Tomcat/TomEE.

How to Setup a Tomcat/TomEE Cluster

Dynamic Discovery

1) Change the <Tomcat/TomEE Install Dir>/conf/server.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Server>
  ...
  <Service>
    ...
    <Engine>
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      ...
    </Engine>
  </Service>
</Server>

2) Start Tomcat/TomEE.

Static Discovery

1) Change the <Tomcat/TomEE Install Dir>/conf/server.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Server>
  ...
  <Service>
    ...
    <Engine>
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelStartOptions="3">
        <Channel className="org.apache.catalina.tribes.group.GroupChannel">
          <Interceptor className="org.apache.catalina.tribes.group.interceptors.StaticMembershipInterceptor">
            <Member className="org.apache.catalina.tribes.membership.StaticMember" port="4000" host="host1" uniqueId="{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}" />
            <Member className="org.apache.catalina.tribes.membership.StaticMember" port="4000" host="host2" uniqueId="{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2}" />
          </Interceptor>
        </Channel>
      </Cluster>
      ...
    </Engine>
  </Service>
</Server>

2) Start Tomcat/TomEE.

You can find more details here.

How to Create a Multicast Client/Server with Apache Tribes

Client

import static java.lang.System.out;

import java.io.Serializable;

import org.apache.catalina.tribes.Channel;
import org.apache.catalina.tribes.ChannelListener;
import org.apache.catalina.tribes.Member;
import org.apache.catalina.tribes.group.GroupChannel;

public final class TribesClient {

  public static void main(final String[] args) throws Exception {
    final Channel chan = new GroupChannel();

    chan.start(Channel.DEFAULT);

    chan.addChannelListener(new ChannelListener() {

      public boolean accept(final Serializable msg, final Member sender) {
        return true;
      }

      public void messageReceived(final Serializable msg, final Member sender) {
        out.println(msg);
      }

    });
  }

}

Server

import org.apache.catalina.tribes.Channel;
import org.apache.catalina.tribes.group.GroupChannel;

public final class TribesServer {

  public static void main(final String[] args) throws Exception {
    final Channel chan = new GroupChannel();

    chan.start(Channel.DEFAULT);

    while (true)

      if (chan.hasMembers())
        chan.send(chan.getMembers(), "Simples Assim", Channel.SEND_OPTIONS_MULTICAST);

  }

}

How to Create a Valve for JBoss EAP

SampleValve.java

import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.valves.ValveBase;

public final class SampleValve extends ValveBase {

  @Override
  public void invoke(final Request request, final Response response) throws Exception {
    getNext().invoke(request, response);
  }

}

jboss-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
  <valve>
    <class-name>SampleValve</class-name>
  </valve>
</jboss-web>