SyntaxHighlighter

Showing posts with label Tomcat. Show all posts
Showing posts with label Tomcat. Show all posts

Wednesday, July 9, 2014

HttpSession Failover - Spring Session

HttpSession Failover - Spring Session

Spring Session 1.0.0M1 was announced recently and it's a great answer to HttpSession failover.  


essentially it allows you to architect this;

- multiple instances of a webapp connected by redis (or any other bus) to share httpsession objects

the trick to remember is that you no longer need sticky sessions; just hit any server you like as the session data is replicated and shared to all members talking to the same redis server.

put this on CloudFoundry and you're really cooking; scale up and down the web apps without any worries about HttpSession loss and stickiness


(and to see it in a really really rough test, check out redis-session.cfapps.io)

Friday, July 12, 2013

Exposing Active Profiles as a JNDI value in Tomcat

Exposing Active Profiles as a JNDI value in Tomcat

here's a quick one for exposing Active Profiles in JNDI for Tomcat.  the use case is when you don't want to set an Active Profile in web.xml (or webappcontextinitializer) but you need them for Tomcat.  (say you're developing against a Tomcat container, but deploying against something else)

so, here's the class

package de.incompleteco.spring.tomcat;

import java.util.Enumeration;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.Name;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;


public class ActiveProfilesObjectFactory implements ObjectFactory {

	static final String VALUE = "value";
	static final String DELIMITER = ",";
	
	@Override
	public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable < ?, ? > environment) throws Exception {
		//init
		String values = null;
		//retrieve
		Reference reference = (Reference) obj;
		Enumeration < RefAddr > addresses = reference.getAll();
		while (addresses.hasMoreElements()) {
			RefAddr address = addresses.nextElement();
			String attributeName = address.getType();
			String attributeValue = address.getContent().toString();
			if (attributeName.equalsIgnoreCase(VALUE)) {
				values = attributeValue;
				break;
			}//end if
		}//end while
		if (values != null) {
			return values.split(DELIMITER);
		}//end if
		return null;//default --> no active profiles
	}

}


and here's how you would access it in Tomcat.  (remember, the location is [your app]/META-INF/context.xml)




	
		

Monday, December 24, 2012

Tomcat as an Infrastructure Provider (in Test)

Tomcat as an Infrastructure Provider (in Test)

i had an interesting requirement come across my desk the other day for a testing scenario in Tomcat that involved a 'foolish' integration test.  that is, 2x applications deployed in Tomcat sharing a single database and a JMS queue server.  so, i tried the usual H2 and AMQ including their infrastructure pieces but didn't have any joy.  generally speaking, startup order got me as both applications expected the infrastructure to be there, and when trying to have them 'create the missing components', it failed.

so, to counter that, a colleague suggested using the Tomcat container itself to initialize the infrastructure before the applications.  this kind of brings it inline with the production environment so we had a dig into it.  one of the things that struck me is that we were trying to piece together technology pieces that are quite easy and accessible in Spring (multiple implementations of a common interface, database script coordination and installation).  so, i've taken the concept a little further and created a simple listener project up on github called catalina-spring-listener. 


the basis behind the project is that the tomcat container initializes an application context upon the configure event in it's lifecycle.  this allows the application context to startup various services and access all Springs goodness as a separate app before anything else starts up.
so, why Spring in a lifecycle? why not a separate WAR? mainly startup order.  by having just tow different machines available (one vastly slower than the other) we could effectively demonstrate that startup order was unreliable and can unwittingly generate race conditions.  by why Spring?  why re-invent the wheel?  Spring's got most of these things in place and most of the infrastructure vendors have spring hooks.  so, launch an app context, and then let Spring do the rest.


if you want the project to look at, it's here… 
https://github.com/incomplete-code/catalina-spring-listener

Friday, September 21, 2012

WebSphere Gotcha - Streaming in WAS 8

WebSphere Gotcha - Streaming in WAS 8

here's a little gotcha in WebSphere 8; if you're using Spring MVC and you decide to serve non-jsp content from behind the WEB-INF directory (e.g. a javascript file), the ViewResolver must return it as a jsp page, otherwise WAS will block it.

In tomcat or other servlet container it will work, but in WebSphere, I can't get it to.

My workaround

create a controller that will stream it back in the HttpServletResponse object