HTTP Session Repository

  • Released
    2018-10-10
  • Version
    1.5.0
  • Authors
    JLupin
  • Link

Description

Microservice http-session-repository moves your http sessions to external microservice. This way for example when you restart your microservices which are using servlet container your sessions will be sustained.

Installation

Just upload microservice zip (http-session-repository.zip) to JLupin Platform. To do so put microservice zip inside upload directory in your platform directory. Then upload it to server with command microservice upload http-session-repository.zip and that’s all.

Usage

Inside your web app microservice put http-session-repository-plugin with spring-session as a

dependencies:

<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session</artifactId>
</dependency>
<dependency>
    <groupId>com.jlupin.microservice.partofjlupin</groupId>
    <artifactId>http-session-repository-plugin</artifactId>
    <version>${http.session.repostiory.plugin.version}</version>
</dependency>

Then enable remote sessions by adding below annotation to your Spring configuration file. It is very important to add this properly or remote sessions won't work.

@EnableJLupinHttpSessionRepository

Also make sure that JLupinDelegator bean is defined in your configuration (definition must be uniquue for @Autowired annotation so make sure you defined only one bean of this type or mark one definition as @Primary).

Example configuration:

@EnableJLupinHttpSessionRepository
public class TestServletSpringConfiguration {
    @Bean
    public JLupinDelegator getJLupinDelegator() {
        return JLupinClientUtil.generateInnerMicroserviceLoadBalancerDelegator(PortType.JLRMC);
    }
}

Adding this component scan annotation provides JLupin's SessionRepository bean which is used by Spring Boot for managing http sessions.

Description how Spring HTTP Session works: here.

Configuration

You can change your sessions max inactive interval by setting property "jlupin.http.seesion.repository.maxInactiveInterval" to number of seconds you want in your microservice.

You can also change interval time which is used by http-session-repository to check for expired sessions to remove them from memory. To do so set property "session.repository.check.rate" to number of milliseconds you want. By default this interval is set to 5 minutes.

How to access HTTP Session object in your code?

A few examples are shown below.

1. Autowire HttpSession in your controller.

@Controller
public class IndexController {
    @Autowired
    private HttpSession httpSession;
    // controller's methods
}

2. Autowire HttpSession as controller method argument:

@Controller
public class IndexController {
    @GetMapping("/")
    public String example(@Autowired HttpSession httpSession) {
        // method body
    }
}

3. In plain servlet get it from HttpServletRequest object.

@WebServlet(urlPatterns = "/example")
public class ExampleServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        final HttpSession session = req.getSession();
        // method body
    }
}

You can see example application on our github: link.

That’s all. From now all your sessions will be handled by remote bean (also sessions used by plain servlets!).

RATE & DISCUSS (0)

No comments found.