小编iva*_*kov的帖子

Spring WebFlux 检测客户端断开连接

假设如下@RestController

@GetMapping("listen")
public Flux<Object> listen() {
    return Flux.create(sink -> process(sink));
}
Run Code Online (Sandbox Code Playgroud)

还有某处

sink.next(new Object());
Run Code Online (Sandbox Code Playgroud)

此代码没有有关接收器状态或完成的信息

尝试使用isCanceled,它每次都返回 false。

是否可以检测到FluxSink客户端仍在使用?

spring flux server-sent-events spring-webflux

7
推荐指数
1
解决办法
3239
查看次数

自我在构造函数中为对象分配对象

假设我们有:

课程Item:

public class Item {
    private Type type;

    Item(Type type) {
        this.type = type;

        if (type == Type.PISTOL || type == Type.AR || type == Type.SNIPER_RIFLE) {
            this = new Weapon(type);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

和类Weapon继承自Item:

public class Weapon extends Item {

    Bullet.Type bulletType;
    int fireRate;

    public Weapon(Type type) {
        this.type = type;
    }
}
Run Code Online (Sandbox Code Playgroud)

它从某个地方调用,如:

Item item = new Item(Item.Type.PISTOL);
Run Code Online (Sandbox Code Playgroud)

我实际上知道this在Java中不可分配,但我想知道如何解决这种情况.

如果它的类型合适,我想分配item新的Weapon.

java inheritance constructor variable-assignment

0
推荐指数
1
解决办法
66
查看次数