com.sishuok.chapter3.web.controller.CallableController.java Source code

Java tutorial

Introduction

Here is the source code for com.sishuok.chapter3.web.controller.CallableController.java

Source

/**
 * Copyright (c) 2005-2012 https://github.com/zhangkaitao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.sishuok.chapter3.web.controller;

import com.sishuok.chapter3.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.concurrent.Callable;

/**
 * spring?
 *   1???Executor 
 *   2???RequestMappingHandlerAdapterServletInvocableHandlerMethod????
 * <p>User: Zhang Kaitao
 * <p>Date: 13-7-16 ?7:59
 * <p>Version: 1.0
 */
@Controller
public class CallableController {

    @RequestMapping("/callable1")
    public Callable<String> callable1(final Model model) {
        return new Callable<String>() {
            @Override
            public String call() throws Exception {
                Thread.sleep(2L * 1000); //?
                String viewName = "msg";
                model.addAttribute("msg", "hello callable");
                return viewName; //??
            }
        };
    }

    @RequestMapping("/callable2")
    public Callable<ModelAndView> callable2() {
        return new Callable<ModelAndView>() {
            @Override
            public ModelAndView call() throws Exception {
                Thread.sleep(2L * 1000); //?
                ModelAndView mv = new ModelAndView("msg");
                mv.addObject("msg", "hello callable");
                return mv;
            }
        };
    }

    @RequestMapping("/callable3")
    @ResponseBody
    public Callable<Object> callable3() {
        return new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                Thread.sleep(2L * 1000); //?
                return new User(1, "zhang");
            }
        };
    }
}