即使应该默认情况下也不启用异步支持

时间:2017-11-15 10:32:19

标签: java spring google-app-engine spring-boot

我有一个使用spring boot和google app引擎构建的应用程序。我已经尝试按照这个https://golb.hplar.ch/p/Server-Sent-Events-with-Spring教程实现服务器发送事件了。 当我构建并尝试访问创建sse-emitter的端点时,我收到以下错误

   java.lang.IllegalStateException: Async support must be enabled on a servlet and for all filters involved in async request processing. This is done in Java code using the Servlet API or by adding "<async-supported>true</async-supported>" to servlet and filter declarations in web.xml.

这很奇怪,因为spring-boot应该默认启用异步支持https://github.com/spring-projects/spring-boot/issues/1665,如本期所述。

我已经尝试添加以下代码,这些代码在某些stackoverflow线程上提出但仍然存在同样的问题。有没有解决这个错误的方法?

@Bean
public ServletRegistrationBean dispatcherServlet() {
    ServletRegistrationBean registration = new 
    ServletRegistrationBean(new DispatcherServlet(), "/");
    registration.setAsyncSupported(true);
    return registration;
}

值得补充的是,我在尝试实现websockets时遇到了同样的问题,后来我放弃了这个问题,转而支持服务器发送的事件。 这是代码:

的pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <!-- Exclude Tomcat so that it doesn't conflict w/ Jetty server -->
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.google.appengine</groupId>
        <artifactId>appengine-api-1.0-sdk</artifactId>
        <version>1.9.54</version>
    </dependency>
    <dependency>                        <!-- Google Core Libraries for Java -->
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>  <!-- https://github.com/google/guava/wiki -->
        <!-- Guava v21.0 doesn't support Java7 -->
        <version>20.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>                        <!-- Google Cloud Client Library for Java -->
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-storage</artifactId>
        <version>1.3.1</version>
    </dependency>
    <!-- Exclude any jul-to-slf4j -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jul-to-slf4j</artifactId>
        <scope>provided</scope>
    </dependency>
    <!-- Include Servlet API -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-datastore</artifactId>
        <version>1.4.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.1</version>
     </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
        <version>1.5.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security.oauth</groupId>
        <artifactId>spring-security-oauth2</artifactId>
        <version>2.2.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.social</groupId>
        <artifactId>spring-social-core</artifactId>
        <version>1.1.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session</artifactId>
        <version>1.3.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.18</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.webjars.bower</groupId>
        <artifactId>jquery</artifactId>
        <version>2.1.3</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.3.1</version>
    </dependency>
</dependencies>

Java代码:

@SpringBootApplication
@EnableOAuth2Sso
public class SpringBootExampleApplication extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
            .and()
                .authorizeRequests()
                    .anyRequest() // This is just temporary. Otherwise only "/" is permited
                        .permitAll();
    }
    public static void main(String[] args) {
        SpringApplication.run(SpringBootExampleApplication.class, args);
    }
}

控制器:

@Controller
public class SSEController {
    private final CopyOnWriteArrayList<SseEmitter> emitters = new CopyOnWriteArrayList<>();
    @GetMapping("/memory")
    public SseEmitter handle() {
        SseEmitter emitter = new SseEmitter();
        // SseEmitter emitter = new SseEmitter(180_000L);
        this.emitters.add(emitter);
        emitter.onCompletion(() -> this.emitters.remove(emitter));
        emitter.onTimeout(() -> this.emitters.remove(emitter));
        return emitter;
    }
    @EventListener
    public void onMemoryInfo(MemoryInfo memoryInfo) {
        List<SseEmitter> deadEmitters = new ArrayList<>();
        this.emitters.forEach(emitter -> {
            try {
                emitter.send(memoryInfo);
            }
            catch (Exception e) {
                deadEmitters.add(emitter);
            }
        });
        this.emitters.removeAll(deadEmitters);
    }
}

预定服务:

@Service
public class MemoryObserverJob {
  public final ApplicationEventPublisher eventPublisher;
  public MemoryObserverJob(ApplicationEventPublisher eventPublisher) {
    this.eventPublisher = eventPublisher;
  }
  @Scheduled(fixedRate = 1000)
  public void doSomething() {
    MemoryInfo mi = new MemoryInfo(123L, 123L);
    this.eventPublisher.publishEvent(mi);
  }
}

邮件的模板:

public class MemoryInfo {
    private final long heap;
    private final long nonHeap;
    private final long ts;
    public MemoryInfo(long heap, long nonHeap) {
        this.ts = System.currentTimeMillis();
        this.heap = heap;
        this.nonHeap = nonHeap;
    }
    public long getHeap() {
        return this.heap;
    }
    public long getNonHeap() {
        return this.nonHeap;
    }
    public long getTs() {
        return this.ts;
    }
}

1 个答案:

答案 0 :(得分:1)

使用@EnableAsync

注释您的BootApplication
@SpringBootApplication
@EnableAsync
@EnableOAuth2Sso
public class SpringBootExampleApplication 

然后在您想要异步的方法中添加另一个。

@Async
public void methodToAsync(){...}