title: Spring Integration manually publish message to channel
body: <p>I'm in the process of learning how to use the Java Spring Framework and started experimenting with Spring Integration. I'm trying to use Spring Integration to connect my application to an MQTT broker both to publish and subscribe to messages but I'm having trouble finding a way to manually publish messages to an outbound channel. If possible I want to build it using notations in the java code exclusively rather than xml files defining beans and other related configuration.</p>
  
  <p>In every example I've seen the solution to manually publishing a message seems to be to use a MessagingGateway Interface and then use the SpringApplicationBuilder to get the ConfigurableApplicationContext to get a reference to the gateway interface in the main method. The reference is then used to publish a message. Would it be possible to use AutoWired for the interface instead? In my attempts I just get a NullPointer.</p>
  
  <p>My aim is to build a game where I subscribe to a topic to get game messages and then whenever the user is ready to make the next move, publish a new message to the topic.</p>
  
  <p><strong>Update</strong>:
  This is one of the examples I've been looking at of how to setup an outbound channel: <a href="https://docs.spring.io/spring-integration/reference/html/mqtt.html" rel="nofollow noreferrer">https://docs.spring.io/spring-integration/reference/html/mqtt.html</a></p>
  
  <p><strong>Update 2 after answer from Gary Russel:</strong></p>
  
  <p>This is some example code I wrote after looking at examples which gets me a NullPointer when using @AutoWired for the Gateway when running gateway.sendToMqtt in Controller.java.  What I want to achieve here is to send an mqtt message manually when a GET request is handled by the controller.</p>
  
  <p>Application.java</p>
  
  <pre><code>@SpringBootApplication
  public class Application {
  
      public static void main(String[] args){
          SpringApplication.run(Application.class, args);
      }
  }
  </code></pre>
  
  <p>Controller.java</p>
  
  <pre><code>@RestController
  @RequestMapping("/publishMessage")
  public class Controller {
  
      @Autowired
      static Gateway gateway;
  
      @RequestMapping(method = RequestMethod.GET)
      public int request(){
          gateway.sendToMqtt("Test Message!");
          return 0;
      }
  }
  </code></pre>
  
  <p>MqttPublisher.java</p>
  
  <pre><code>@EnableIntegration
  @Configuration
  public class MqttPublisher {
      @Bean
      public MqttPahoClientFactory mqttClientFactory(){
          DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
          factory.setServerURIs("tcp://localhost:1883");
          return factory;
      }
  
      @Bean
      @ServiceActivator(inputChannel = "mqttOutboundChannel")
      public MessageHandler mqttOutbound(){
          MqttPahoMessageHandler messageHandler =
                  new MqttPahoMessageHandler("clientPublisher", mqttClientFactory());
          messageHandler.setAsync(true);
          messageHandler.setDefaultTopic("topic");
          return messageHandler;
      }
  
      @Bean
      public MessageChannel mqttOutboundChannel(){
          return new DirectChannel();
      }
  
      @MessagingGateway(defaultRequestChannel = "mqttOutboundChannel")
      public interface Gateway {
  
          void sendToMqtt(String data);
      }
  }
  </code></pre>
  
  <p><strong>Update</strong>:</p>
  
  <p>Not sure if this is the proper logging but it is what I get from adding:</p>
  
  <pre><code>logging.level.org.springframework.web=Debug
  logging.level.org.hibernate=Error
  </code></pre>
  
  <p>to application.properties.</p>
  
  <p><a href="https://hastebin.com/cuvonufeco.hs" rel="nofollow noreferrer">https://hastebin.com/cuvonufeco.hs</a></p>