调用java中的非静态消息传递函数



我正在尝试使用STOMP和websockets从我的应用程序中的任何地方发送消息。然而,我遇到了麻烦,因为我不能使"greet"方法静态,因为"this"。模板"。那么我就不能调用这个方法了。我如何解决这个问题?

这是我的控制器类:

@Controller

公共类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 + "!");
}

在replier方法中调用greet(reply)无效,因为我不能对非静态方法进行静态调用。我如何呼叫问候并让消息发送?

我不明白为什么你认为greeting需要是静态的。

我在websocket文档中找到了这个:

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

尝试使greeting不是静态的。如果您在使用非静态方法时遇到问题,请告诉我们。

最新更新