Cześć
Próbuję w swoim kodzie skonfigrować połączenie z RabbitMq, wysłać prostą wiadomość i sprawdzić czy poprawnie wszystko działa.
Aplikacja wywala się i dostaję informację Connection refused: connect. Nie mam doświadczenia z Rabbitem. Próbowałem różnych połączeń ale bez skutku
Poniżej podsyłam kod, proszę rzucić okiem. Może gdzieś w konfiguracji jest błąd.
@RequiredArgsConstructor
@Slf4j
@Profile(value = {"dev"})
@Service
@ConditionalOnProperty(
value = "spring.rabbitmq.retail.enabled",
havingValue = "true")
public class Config {
static final String topicExchangeName = "amp.topic";
static final String queueName = "PDC";
@Bean
Queue queue() {
return new Queue(queueName, true);
}
@Bean
TopicExchange exchange() {
return new TopicExchange(topicExchangeName);
}
@Bean
public RabbitTemplate rabbitTemplate(Jackson2JsonMessageConverter jackson2JsonMessageConverter, ConnectionFactory connectionFactory)
var rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(jackson2JsonMessageConverter);
rabbitTemplate.setChannelTransacted(true);
return rabbitTemplate;
}
@Bean
ConnectionFactory connectionFactory() throws Exception {
RabbitConnectionFactoryBean factory = new RabbitConnectionFactoryBean();
factory.setHost("10.151.101.193");
factory.setPort(4443);
factory.setVirtualHost("ic.out");
factory.setUsername("retail");
factory.setPassword("edd");
factory.setUseSSL(false);
factory.afterPropertiesSet();
return new CachingConnectionFactory(Objects.requireNonNull(factory.getObject()));
}
@Bean
public Jackson2JsonMessageConverter jackson2JsonMessageConverter() {
return new Jackson2JsonMessageConverter(objectMapper());
}
public ObjectMapper objectMapper() {
var objectMapper = new ObjectMapper();
return objectMapper;
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.ServerResponse;
import static org.springframework.web.servlet.function.RouterFunctions.route;
import static org.springframework.web.servlet.function.ServerResponse.ok;
@Configuration
public class Controller {
@Bean
public RouterFunction<ServerResponse> productListing(Producer producer) {
return route().GET("/send", req -> {
producer.sendMessage();
return ok().build();
}).build();
}
}
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;
import java.util.logging.Logger;
@Component
public class Producer {
Logger log = LoggerFactory.getLogger(Producer.class);
private final RabbitTemplate rabbitTemplate;
public Producer(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
public void sendMessage() {
TestClass testClass = TestClass.builder()
.data("**81*")
.build();
log.info("Sending message to queue: {}", testClass);
rabbitTemplate.convertAndSend(testClass);
}
}
@Data
@AllArgsConstructor
@Builder(tuBoilder = true)
public class TestClass implements Serrializable {
@JsonProperty("data")
String data;
}
@SpringBootApplication
public class RabbitmqApplication {
public RabbitmqApplication(Producer producer) {
this.producer = producer;
}
public static void main(String[] args) {
SpringApplication.run(RabbitmqApplication.class, args);
}
private final Producer producer;
@EventListener(ApplicationReadyEvent.class)public void doSomethingAfterStartup() {
producer.sendMessage();
System.out.println("Hello world");
}
}