Spring boot 我希望使用@Scheduler注释在rabbitMQ中发送延迟为5秒的消息
我在做什么::我实际上是从postgres表中一次获取5个值(在一个块中),并存储这5个值并将其发送到rabbitMQ{我能做到}Spring boot 我希望使用@Scheduler注释在rabbitMQ中发送延迟为5秒的消息,spring-boot,spring-amqp,spring-rabbit,Spring Boot,Spring Amqp,Spring Rabbit,我在做什么::我实际上是从postgres表中一次获取5个值(在一个块中),并存储这5个值并将其发送到rabbitMQ{我能做到} 我想要的是在发送一个数据块(一次5行表)之后,我需要创建5秒的延迟,然后发送下一个数据块。 错误:错误是因为我在accessdatajpaaapplication.java中使用了@Scheduler注释。我需要知道这两个注释@EnableSchedule和@Scheduled(fixedDelay=5000L)应该放在哪里注意:我也知道为什么会出错,但我需要一
我想要的是在发送一个数据块(一次5行表)之后,我需要创建5秒的延迟,然后发送下一个数据块。
错误:错误是因为我在accessdatajpaaapplication.java中使用了@Scheduler注释。我需要知道这两个注释@EnableSchedule和@Scheduled(fixedDelay=5000L)
应该放在哪里注意:我也知道为什么会出错,但我需要一个解决方案。如我所知,如果没有书面参数,可以使用@Scheduled注释,然后告诉我应该将其放置在该程序中的何处。实际上,我想延迟访问datajpaapplaication.java中的sendMessage函数。或者,如果不可能,请在这里告诉我如何在spring boot应用程序中使用延迟消息交换插件。我使用的是最新的spring版本
代码:发件箱
package com.example.demo;
import com.fasterxml.jackson.annotation.JsonProperty;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.io.Serializable;
@Entity
public class OutBox implements Serializable {
@Id
private String id;
private String aggregrate;
private String operation;
private String message;
public OutBox() {
}
public OutBox(@JsonProperty("id") String id, @JsonProperty("aggregrate") String aggregrate, @JsonProperty("operation") String operation, @JsonProperty("message") String message) {
this.id=id;
this.aggregrate=aggregrate;
this.operation=operation;
this.message=message;
}
@Override
public String toString() {
return String.format(
"OutBox{ id='%s', aggregrate='%s', operations='%s', message='%s' }",
id, aggregrate, operation, message);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAggregrate() {
return aggregrate;
}
public void setAggregrate(String aggregrate) {
this.aggregrate = aggregrate;
}
public String getOperation() {
return operation;
}
public void setOperation(String operation) {
this.operation = operation;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
代码:OutBoxRepository
错误:
org.springframework.beans.factory.BeanCreationException:创建名为“accessingDataJpaApplication”的bean时出错[C:\Users\Akand\Downloads\rabbitMQTable\demo\demo\target\classes\com\example\demo\accessingDataJpaApplication.class]:bean初始化失败;嵌套异常为java.lang.IllegalStateException:遇到无效的@Scheduled方法“sendMessage”:只有没有参数方法可以用@Scheduled进行注释 在
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:603)~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] 在
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] 在
org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] 在
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226)~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] 在
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] 在
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] 在
org.springframework.beans.factory.support.DefaultListableBeanFactory.PreInstanceSingleton(DefaultListableBeanFactory.java:895)~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] 在
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE] 在
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE] 在org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE] 在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE] 在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE] 位于org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE] 在org.springframework.boot.SpringApplication.run(SpringApplication.java:315)[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE] 在org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE] 在org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE] 在com.example.demo.DemoApplication.main(DemoApplication.java:12)[classes/:na] 原因:java.lang.IllegalStateException:遇到无效的@Scheduled方法“sendMessage”:只有任何arg方法可以用@Scheduled注释 在org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.processScheduled(ScheduledAnnotationBeanPostProcessor.java:499)~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE] 在org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$null$1(ScheduledAnnotationBeanPostProcessor.java:362)~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE] 在java.lang.Iterable.forEach(Iterable.java:75)~[na:1.8.0_241] 在org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$postProcessAfterInitialization$2(ScheduledAnnotationBeanPostProcessor.java:362)~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE] 在java.util.LinkedHashMap.forEach(LinkedHashMap.java:684)~[na:1.8.0\u 241] 在org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(ScheduledAnnotationBeanPostProcessor.java:361)~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:431)~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] 位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowir
package com.example.demo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
public interface OutBoxRepository extends JpaRepository<OutBox, String> {
Page<OutBox> findAll(Pageable pageable);
}
package com.example.demo;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class RabbitMQConfig {
@Value("${javainuse.rabbitmq.queue}")
String queueName;
@Value("${javainuse.rabbitmq.exchange}")
String exchange;
@Value("${javainuse.rabbitmq.routingkey}")
private String routingkey;
@Bean
Queue queue() {
return new Queue(queueName, false);
}
// @Bean
// CustomExchange delayExchange() {
// Map<String, Object> args = new HashMap<String, Object>();
// args.put("x-delayed-type", "direct");
// return new CustomExchange("my-exchange", "x-delayed-message", true, false, args);
// }
@Bean
DirectExchange exchange() {
return new DirectExchange(exchange);
}
@Bean
Binding binding(Queue queue, DirectExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(routingkey);
}
@Bean
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
public AmqpTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(jsonMessageConverter());
return rabbitTemplate;
}
// @Bean
// public AmqpTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
// final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
// rabbitTemplate.setMessageConverter(jsonMessageConverter());
// return rabbitTemplate;
// }
}
package com.example.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.utils.SerializationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import java.util.List;
import java.util.Optional;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@SpringBootApplication
public class AccessingDataJpaApplication {
private static final Logger log = LoggerFactory.getLogger(AccessingDataJpaApplication.class);
@Autowired
private AmqpTemplate rabbitTemplate;
@Autowired
private OutBoxRepository repository;
@Value("${javainuse.rabbitmq.exchange}")
private String exchange;
@Value("${javainuse.rabbitmq.routingkey}")
private String routingkey;
public static void main(String[] args) {
SpringApplication.run(AccessingDataJpaApplication.class);
}
@Scheduled(fixedRate=5000)
public void sendMessage(List<OutBox> message) {
log.info("Sending message...");
// rabbitTemplate.convertAndSend(exchange, routingkey,SerializationUtils.deserialize(message));
for(int i=0;i<message.size();i++)
rabbitTemplate.convertAndSend(exchange,routingkey, message.get(i));
}
@Bean
public CommandLineRunner demo(OutBoxRepository repository) {
return (args) -> {
repository.save(new OutBox("fsks-ghty-eryr-jghd","OO_FLOW_SCHEDULES","UPDATE","{ \"brand\" : \"Mercedes\", \"doors\" : 5 }"));
repository.save(new OutBox("fsks-bnmb-eryr-jghd","OO_FLOW_ENTITY","UPDATE","{ \"brand\" : \"BMW\", \"doors\" : 7 }"));
log.info("Customers found with findAll():");
log.info("--------------PAGE: 0-----------------");
int count = 0;
List<OutBox> lst=null;
for (OutBox outbox : repository.findAll()) {
Page<OutBox> u = repository.findAll(PageRequest.of(count, 5));
lst=u.getContent();
sendMessage(lst);
log.info(outbox.toString());
count ++;
}
log.info("");//log is to used for printing in console
Optional<OutBox> outbox = repository.findById("fsks-ghty-eryr-jite"); // L means of type long
log.info("Customer found with findById(1L):");
log.info("--------------------------------");
log.info(outbox.toString());
};
}
}
{"id":"fsks-ghty-eryr-jghd","aggregrate":"OO_FLOW_SCHEDULES","operation":"UPDATE","message":"{ \"brand\" : \"Mercedes\", \"doors\" : 5 }"}