title: Spring Websocket MessageSendingOperation not working without @Scheduled
body: <pre><code>.
  .
  .
  @Autowired
  private MessageSendingOperations&lt;String&gt; messagingTemplate;
  
  
  @Scheduled(fixedDelay = 5000)
  public void sendMessage(){
  
             messagingTemplate.convertAndSend("/topic/data","hello"); 
  
      }
  }
  </code></pre>
  
  <p>The above works. but</p>
  
  <pre><code>sendMessage();
  
  public void sendMessage(){
  
             messagingTemplate.convertAndSend("/topic/data", "hello");
  
      }
  }
  </code></pre>
  
  <p>doesn't work, it got java.lang.NullPointerException </p>
  
  <p>Does that mean I could only use  messagingTemplate.convertAndSend(...) inside a function annotated with @Scheduled?</p>
  
  <p>Please help...
  @Deendayal Garg, thanks for your help, I did this this according to your advice:
      public class Communicator implements ApplicationListener {</p>
  
  <pre><code>private final MessageSendingOperations&lt;String&gt; messagingTemplate;
  
  @Autowired
  public Communicator(final MessageSendingOperations&lt;String&gt; messagingTemplate){
      this.messagingTemplate = messagingTemplate;
  }
  
  public void onApplicationEvent(BrokerAvailabilityEvent event) {
      // TODO Auto-generated method stub
  }
  
  
  public void sendMessage(Message data) {
      this.messagingTemplate.convertAndSend("/topic/data",data.getBody());
      System.out.println(new Random().nextInt(100));
    }
  }
  public class WebConfiguration extends WebMvcConfigurerAdapter {
  
  @Override
  public void configureDefaultServletHandling(
      final DefaultServletHandlerConfigurer configurer) {
      configurer.enable();
  }
  
  
  @Bean
  public Communicator communicator(){
      Communicator comm = new Communicator();
      return comm;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Then I injected the bean in the class below</p>
  
  <pre><code>public class StepExecListener implements StepExecutionListener, ApplicationListener&lt;BrokerAvailabilityEvent&gt;{
  @Autowired
  Communicator communicator;
  Message msg = new Message();
  msg.setBody("Twale!");
  msg.setType("test");
  
  communicator.sendDataUpdates(msg);
  }
  </code></pre>
  
  <p>But I still got it got java.lang.NullPointerException after calling communicator.sendDataUpdates(msg);</p>