spring引导rabbitmq amqp和websocket空指针异常

jucafojl  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(426)

我正试图在用户的连接建立之后,在websockethandler中使用rabbitmq发送消息。问题是,rabbitmq生产者bean为空。从控制器调用生产者工作得非常好,但是当我尝试从websockethandler bean调用生产者时,如果失败,则出现空指针异常:
websocket配置:

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    registry.addHandler(new MyWebSocketHandler(), "/connect");
}

}

websocket处理程序bean,其中空指针异常为:

@Component
public class MyWebSocketHandler extends TextWebSocketHandler {
private static final Logger LOG = LoggerFactory.getLogger(WebSocketConfig.class);

@Autowired
QueueProducer producer; //NULL

List<WebSocketSession> sessions = new CopyOnWriteArrayList<>();

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
    sessions.add(session);
    List<String> lol = session.getHandshakeHeaders().get("User-Agent");
    //Calling the rabbitmq producer
    producer.produce("Hi Mark");//NULL
}

rabbitmq配置:

@Configuration
public class RabbitConfiguration {

@Value("${fanout.exchange}")
private String fanoutExchange;

@Value("${queue.name}")
private String queueName;

@Bean
Queue queue() {
return new Queue(queueName, true);
}

@Bean
FanoutExchange exchange() {
return new FanoutExchange(fanoutExchange);
}

@Bean
Binding binding(Queue queue, FanoutExchange exchange) {
return BindingBuilder.bind(queue).to(exchange);
}

}

rabbitmq生产者代码:

@Component
public class QueueProducer {

protected Logger logger = LoggerFactory.getLogger(getClass());

@Value("${fanout.exchange}")
private String fanoutExchange;

private final RabbitTemplate rabbitTemplate;

@Autowired
public QueueProducer(RabbitTemplate rabbitTemplate) {
    super();
    this.rabbitTemplate = rabbitTemplate;
}

public void produce(String message) throws Exception {
    logger.info("Storing notification...");
    rabbitTemplate.setExchange(fanoutExchange);
    rabbitTemplate.convertAndSend(message);
    logger.info("Notification stored in queue sucessfully");
}

}

堆栈跟踪:

java.lang.NullPointerException: null
at com.ta9.common.Config.MyWebSocketHandler.afterConnectionEstablished(MyWebSocketHandler.java:43) ~[classes/:na]
at org.springframework.web.socket.handler.WebSocketHandlerDecorator.afterConnectionEstablished(WebSocketHandlerDecorator.java:70) ~[spring-websocket-5.3.8.jar:5.3.8]
at org.springframework.web.socket.handler.LoggingWebSocketHandlerDecorator.afterConnectionEstablished(LoggingWebSocketHandlerDecorator.java:48) ~[spring-websocket-5.3.8.jar:5.3.8]
at org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator.afterConnectionEstablished(ExceptionWebSocketHandlerDecorator.java:48) ~[spring-websocket-5.3.8.jar:5.3.8]
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.onOpen(StandardWebSocketHandlerAdapter.java:104) ~[spring-websocket-5.3.8.jar:5.3.8]
at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.init(WsHttpUpgradeHandler.java:135) ~[tomcat-embed-websocket-9.0.48.jar:9.0.48]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:940) ~[tomcat-embed-core-9.0.48.jar:9.0.48]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1723) ~[tomcat-embed-core-9.0.48.jar:9.0.48]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.48.jar:9.0.48]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.48.jar:9.0.48]
at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]

先谢谢你。

xytpbqjk

xytpbqjk1#

这是预期的行为。让我们再一次查看您的代码:

registry.addHandler(new MyWebSocketHandler(), "/connect");

你知道你在做什么吗 new 手动。您不需要依赖于这里的依赖注入容器。你的 MyWebSocketHandler 在这种情况下,它不是一个bean。因为它被标记为 @Component 我认为它被正确地扫描了,并作为bean提供给应用程序上下文。因此,您可以这样做来解决您的问题:

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    MyWebSocketHandler myWebSocketHandler;

    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(this.myWebSocketHandler, "/connect");
    }

}

相关问题