0

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?

Theo
  • 883
  • 2
  • 10
  • 19

1 Answers1

2

I don't understand why you think greeting needs to be static.

I found this on the websocket documentation:

@Controller
public class GreetingController {

    @MessageMapping("/greeting") {
    public String handle(String greeting) {
        return "[" + getTimestamp() + ": " + greeting;
    }
}

Try making greeting not static. If you have problems with a nonstatic method please let us know what they are.

  • Hi Matthew! Thanks for getting back to me. Sorry for the confusing method names, but what I'd like to be able to call from anywhere is the "greet" method, rather than the "greeting" method. Do you have any advice? – Theo Jul 14 '14 at 14:36
  • I want to be able to call the greet method from any Class in my application, so doesn't it need to be static? – Theo Jul 14 '14 at 17:16
  • You could make the template variable static, and then the greet method could also be static. Or you could make both of them specific to the instance. There's no way to do it both ways. See [Java: when to use static methods](http://stackoverflow.com/questions/2671496/java-when-to-use-static-methods). – Matthew Jacobs Jul 16 '14 at 01:15
  • You can also make a static variable of type `GreetingController`, or you could use the [singleton](http://www.javaworld.com/article/2073352/core-java/simply-singleton.html) pattern. This way you could easily access an instance of type `GreetingController` from anywhere in your application. – Matthew Jacobs Jul 16 '14 at 01:20