I am trying to send messages from anywhere in my application using STOMP and websockets. However, I am having trouble because I cannot make the "greet" method static because of the "this.template" inside the method. Then I cannot make a call to the method. How can I fix this issue?
Here is my Controller class:
@Controller
public class HelloController {
@Autowired
private SimpMessagingTemplate template;
@Autowired
public HelloController(SimpMessagingTemplate template) {
this.template = template;
}
public HelloController() {
}
public static void replier(String reply) {
greet(reply);
}
@RequestMapping(value="/hello", method=RequestMethod.POST)
public void greet(String greeting) {
Greeting text = new Greeting("Goodbye, " + greeting + "!");
this.template.convertAndSend("/topic/greetings", text);
}
@RequestMapping(value="/", method=RequestMethod.GET)
public String index() {
return "index";
}
@MessageMapping("/hello")
@SendTo("/queue/greetings")
public static Greeting greeting(HelloMessage message) throws Exception {
System.out.println("Sending message...");
beginRoute(message.getName());
return new Greeting("Hello, " + message.getName() + "!");
}
@SendTo("/queue/informer")
public static Greeting beginRoute(String message) {
Application.startBody(message);
//System.out.println("Returning from second message!");
return new Greeting("So long, " + message + "!");
}
The call of greet(reply) in the replier method is invalid because I cannot make a static call to a non-static method. How can I call greet and get the message sent?