我最近开始了一个处理rabbitmq,websockets和推送通知的项目。我已经完成了在侦听器和发送器之间的Rabbitmq通信,但是现在我仍然无法理解在websocket / push通知部分的应用程序中要添加什么。我在线上研究了不同的教程,但是这些教程并不完全匹配,我对如何继续进行感到困惑。
我的项目假设我从文件中读取了pacient的活动列表,并且每3或5秒将其中的一行发送到Web部件以进一步显示(我也有一些活动规则)。我已经设法用rabbitmq + amqp进行了大多数发送,现在我在为之后的工作而苦苦挣扎。如何添加网络套接字以及如何连接它们?
下面是我的Rabbitmq侦听器类:
package assignment2.Ass;
////spirng messaging is in the tutorial for the first part
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
@Service
public class PracticalTipListener {
private static final Logger log = LoggerFactory.getLogger(PracticalTipListener.class);
@RabbitListener(queues = AssApplication.DEFAULT_PARSING_QUEUE)
public void consumedDefaultMesage(final MessageDefinition message) {
log.info("Message Received" + message.toString());
Rules applyRules= new Rules(message);
}
}
发件人
package assignment2.Ass;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
public class PracticalTipSender {
private static final Logger log = LoggerFactory.getLogger(PracticalTipSender.class);
int count=0;
private final RabbitTemplate rabbitTemplate;
FileReading fl= new FileReading();
List<String> listaAct = fl.listaDespartita;
public PracticalTipSender(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
@Scheduled(fixedDelay = 3000L,initialDelay = 5000L)
public void send() {
String start= fl.dataMaker(listaAct.get(count),listaAct.get(count+1));
String end= fl.dataMaker(listaAct.get(count+2),listaAct.get(count+3));
String act=listaAct.get(count+4);
System.out.println(start+end+act);
MessageDefinition mes= new MessageDefinition("1",start,end,act);
System.out.println(mes);
count=count+5;
rabbitTemplate.convertAndSend(AssApplication.EXCHANGE_NAME, AssApplication.ROUTING_KEY, mes);
log.info("message sent");
}
//
}
这是我连接到rabbtitmq服务器并创建队列的方法
@SpringBootApplication
@EnableScheduling
public class AssApplication {
public static final String EXCHANGE_NAME = "tips_tx";
public static final String DEFAULT_PARSING_QUEUE = "default_parser_q";
public static final String ROUTING_KEY = "tips";
public static void main(String[] args) {
SpringApplication.run(AssApplication.class, args);
}
@Bean
public TopicExchange tipsExchange() {
return new TopicExchange((EXCHANGE_NAME));
}
@Bean
public Queue defaultParsingQueue() {
return new Queue(DEFAULT_PARSING_QUEUE);
}
@Bean
public Binding queueTopicExchange() {
return BindingBuilder.bind(defaultParsingQueue()).to(tipsExchange()).with(ROUTING_KEY);
}
@Bean
public Jackson2JsonMessageConverter produceJSON()
{
return new Jackson2JsonMessageConverter();
}
@Bean
public RabbitTemplate rabbitTemplateConf(final ConnectionFactory conect)
{
RabbitTemplate rory= new RabbitTemplate(conect);
rory.setMessageConverter(produceJSON());
return rory;
}
FileReading是一个类,我在其中读取输入文件,而messagedefinition是我的消息的模型类(消息为JSON格式),规则是该活动的一些规则。
对于如何进一步进行一些帮助将不胜感激。 谢谢 !