0

We are using a SimpleUrlHandlerMapping in our Spring Boot 2.1 application to load mapping information from database:

@Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
    SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
    simpleUrlHandlerMapping.setOrder(Ordered.HIGHEST_PRECEDENCE);
    simpleUrlHandlerMapping.setInterceptors(requestMonitoringInterceptor);

    Map<String, Object> urlMap = getUrlMapFromDb();
    simpleUrlHandlerMapping.setUrlMap(urlMap);

    return simpleUrlHandlerMapping;
}

It works fine, but if the mapping is changed then we need to restart the server to load the new mapping during startup. The application administrator doesn't have server access so he/she is not able to restart the application.

Is there any way to reload the mapping from the application itself without restarting the server?

Vmxes
  • 2,329
  • 2
  • 19
  • 34

2 Answers2

0

Reload just the war through tomcat manager gui

Sahil Bhalla
  • 175
  • 1
  • 4
  • I need a solution that works inside the application as the application administrator has no access to any other resources. I have extended my question. – Vmxes Jun 19 '19 at 08:41
  • create a service that will reload this SimpleURLMappingBean or any other bean to whom you are passing this dependency using below code: https://stackoverflow.com/questions/51218086/how-to-reinitialize-a-spring-bean – Sahil Bhalla Jun 21 '19 at 06:44
  • Thanks, I was able to reload properties as proposed in the link, however it doesn't solved the mapping problem. The new mappings are ignored. It seems to me that something else also happens during mapping initialization at startup. – Vmxes Jun 21 '19 at 12:27
  • May be you need to reload another bean that will reload these properties. – Sahil Bhalla Jun 24 '19 at 04:25
0

You don't need to add a new Map for every connection/conversation. The keys in urlMap are Ant path style patterns. So you just need to put into urlMap something like this:

@Autowired
private WebSocketHandler webSocketHandler;

@Bean
public HandlerMapping handlerMapping() {
    Map<String, WebSocketHandler> urlMap = new HashMap<>();
    urlMap.put("/chat/**", webSocketHandler);

    return new SimpleUrlHandlerMapping(urlMap);
}

Then in frontend (android code in example), you subscribe to URI like this:

URI uri = URI.create("ws://10.0.2.2:8080/chat/" + roomId);

Just add a sub path (roomId) and it will become a channel