I am creating a messenger. Android client is written in java and backend is also written in Java with Spring. The client uses okhttp and retrofit. The spring app uses boot, mvc, websocket, jms and broker. I know how to deal with boot and mvc well. But I am just attempting to dive into jms and other stuff. I've been struggling with those things for a while. My context-path is "gleamy". It's my app's name. Everything related to generally boot app and controllers is fine. DAOs work correctly. But there're some issues with websocket and broker configs. I consider them troubles with paths, but I'm just learning jms and sockets and don't know exactly. What it should be:
- Android client establishes connection with spring.
- client sends a request to the controller.
- it should be sent to another client that is in the same chat. I am attempting to send the same msg to all participants.
- client processes the msg
Please, give me a hand to deal with them.
@Configuration
@EnableWebSocketMessageBroker
public class JmsConfig implements WebSocketMessageBrokerConfigurer{
@Override
public void configureMessageBroker(MessageBrokerRegistry registry)
{
registry.setApplicationDestinationPrefixes("/gleamy");
registry.enableSimpleBroker("topic", "queue");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry)
{
registry.addEndpoint("/websocket");
}
@Override
public void configureClientInboundChannel(ChannelRegistration registration)
{
registration.interceptors(new WebSocketInterceptor());
}
}
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer
{
private static final Logger logger = LoggerFactory.getLogger(WebSocketConfig.class);
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry)
{
registry.addHandler(appWebSocketHandler(), "/websocket").setAllowedOrigins("*");
}
@Bean
public WebSocketHandler appWebSocketHandler()
{
return new TextWebSocketHandler(){
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage msg)
{
logger.info(msg.toString());
try{
session.sendMessage(msg);
}
catch(Exception ex)
{
ex.printStackTrace(System.err);
}
}
};
}
}
@Service
@Getter @Setter
public class MessageService {
private final ApplicationContext appContext;
private final JmsTemplate jmsTpl;
private final SimpMessagingTemplate simpJms;
private final Map<String, AbstractDAO> daos;
@Autowired
public MessageService(ApplicationContext appContext, JmsTemplate jmsTpl, SimpMessagingTemplate simpJms)
{
this.appContext = appContext;
this.jmsTpl = jmsTpl;
this.simpJms = simpJms;
daos = new HashMap<>();
addDAO("chatDAO",appContext.getBean("chatDAO", ChatDAO.class));
}
public void addDAO(String name, AbstractDAO dao)
{
daos.put(name, dao);
}
public void notifyChatMembers(MsgInfo msg) {
ChatDAO chatDAO = (ChatDAO)daos.get("chatDAO");
for (Long userid : chatDAO.getUserIdsFromChat(msg.getChatid()))
{
simpJms.convertAndSend("/topic/users/" + userid, msg);
}
}
}
@RestController
@RequestMapping("/messages")
public class MessageController {
private final MessageDAO msgDAO;
private final MessageService msgService;
public MessageController(MessageDAO msgDAO, MessageService msgService)
{
this.msgDAO = msgDAO;
this.msgService = msgService;
}
@PostMapping("/add")
@ResponseBody
public Map<String, Long> addMessage(@RequestBody MsgInfo msg)
{
Map<String, Long> response = new HashMap<>();
long msgid = msgDAO.add(msg);
response.put("msgid", msgid);
msgService.notifyChatMembers(msg);
return response;
}
}