Slide 1

Slide 1 text

S P R I N G S E S S I O N Josh Long (⻰龙之春) @starbuxman [email protected] github.com/joshlong G E T T I N G S TA R T E D W I T H huge thanks to Rob Winch! @rob_winch

Slide 2

Slide 2 text

Spring Developer Advocate Josh Long (⻰龙之春) @starbuxman [email protected] | Jean Claude van Damme! Java mascot Duke some thing’s I’ve authored...

Slide 3

Slide 3 text

@starbuxman

Slide 4

Slide 4 text

@starbuxman is a stinky! • people jam all sorts of nasty state in there (I’m looking at you Java Server Faces!) the Servlet HttpSession..

Slide 5

Slide 5 text

@starbuxman the Servlet HttpSession..

Slide 6

Slide 6 text

@starbuxman is hard to scale: Tomcat the Servlet HttpSession.. http://tomcat.apache.org/tomcat-6.0-doc/cluster-howto.html /Sender> ame="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/> .catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>

Slide 7

Slide 7 text

@starbuxman is hard to scale: Jetty the Servlet HttpSession.. http://www.eclipse.org/jetty/documentation/9.2.3.v20140905/session-clustering-jdbc.html fred javax.sql.DataSource/default 60 jdbcIdMgr

Slide 8

Slide 8 text

@starbuxman nope. • multicast is a huge no-no in most cloud environments • even if it were permitted, most clustering facilities don’t have multi-zone high availability support just works in the cloud tho, right? https://devcenter.heroku.com/articles/intro-for-java-developers

Slide 9

Slide 9 text

@starbuxman some exceptions.. just works in the cloud tho, right? http://blog.pivotal.io/cloud-foundry-pivotal/products/session-replication-on-cloud-foundry-2 • Cloud Foundry supports sticky sessions. • as of late 2014, it also supports session replication for Tomcat and .wars (specifically)

Slide 10

Slide 10 text

@starbuxman a Servlet HttpSession wrapper Spring Session package sample; import org.springframework.session.web.context
 .AbstractHttpSessionApplicationInitializer; /** * web.xml equivalent */ public class Initializer extends AbstractHttpSessionApplicationInitializer { }

Slide 11

Slide 11 text

@starbuxman a Servlet HttpSession wrapper Spring Session package sample; import javax.servlet.*; import javax.servlet.annotation.*; import javax.servlet.http.*; import java.io.IOException; @WebServlet("/session") public class SessionServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String attributeName = req.getParameter("attributeName"); String attributeValue = req.getParameter("attributeValue"); req.getSession().setAttribute(attributeName, attributeValue); // just works! resp.sendRedirect(req.getContextPath() + "/"); } private static final long serialVersionUID = 2878267318695777395L; }

Slide 12

Slide 12 text

@starbuxman multi-platform Spring Session • works for your web container (Tomcat) or classic application server (JBoss, WebSphere, etc) • works in the cloud • doesn’t require Spring
 (I know right?? WHY?)

Slide 13

Slide 13 text

@starbuxman polyglot persistence Spring Session • pluggable implementations: • defaults for Redis, Map • about the Map.. • implies Hazelcast, Coherence,
 Gemfire support

Slide 14

Slide 14 text

@starbuxman Demonstration basic setup

Slide 15

Slide 15 text

@starbuxman HttpSessionStrategy strategies Spring Session • headers (x-auth-token) • cookies (you can ditch JSESSIONID!)

Slide 16

Slide 16 text

@starbuxman Demonstration session strategies

Slide 17

Slide 17 text

@starbuxman works with WebSockets! Spring Session • the standard is utterly broken here. No, seriously. #WTF • no easy way to perpetuate HTTP session from WS handler. As soon as HTTP session dies, so does WS communication. https://java.net/jira/browse/WEBSOCKET_SPEC-175 
 https://issues.apache.org/bugzilla/show_bug.cgi?id=54738

Slide 18

Slide 18 text

@starbuxman Demonstration websockets

Slide 19

Slide 19 text

@starbuxman User Switching (e.g.: Google accounts) Spring Session HttpServletRequest httpRequest = (HttpServletRequest) request; HttpSessionManager sessionManager = (HttpSessionManager) httpRequest.getAttribute(HttpSessionManager.class.getName()); SessionRepository repo = (SessionRepository) httpRequest.getAttribute(SessionRepository.class.getName()); String currentSessionAlias = sessionManager.getCurrentSessionAlias(httpRequest); Map sessionIds = sessionManager.getSessionIds(httpRequest);

Slide 20

Slide 20 text

@starbuxman Demonstration user switching

Slide 21

Slide 21 text

@starbuxman • @Scope(“flash”) 
 UserConfirmation confirmation(){ .. } • @Scope(“session”) 
 ShoppingCart cart (){ … } • two logically different applications can now talk to each other! (e.g.: poor- man’s single-sign on!) Other Use Cases

Slide 22

Slide 22 text

@starbuxman • session concurrency control (“sign me out of other accounts”) • Spring Batch & Integration claim-check • support for managing accounts easier • smarter injectable beans, @MVC arg resolvers, etc. • optimized persistence (alternatives to Java serialization) What’s in the Works

Slide 23

Slide 23 text

Josh Long (⻰龙之春) @starbuxman @springcentral [email protected] github.com/joshlong References spring.io/guides docs.spring.io/spring-session github.com/joshlong/bootiful-sessions Questions? huge thanks to Rob Winch! @rob_winch