I am trying to setup the rabbitmq connection factory in class. Would like to read the property defined in application.properties file using @Value annotation. However, somehow bean configuration is not correct. I am not able to understand that . Here is the code :
@Controller
@Configuration
@RequestMapping("/unittestdata")
@Component
@PropertySource("classpath:application.properties")
public class UnitAmqpRestController {
@Value("${spring.rabbitmq.port}")
public static int mqPort;
public UnitAmqpRestController() {
}
private static final Logger logger = LoggerFactory.getLogger(UnitAmqpRestController.class);
private static RabbitTemplate rabbitTemplate;
private String replyQueueName;
private static Connection connection;
private static Channel channel;
private String requestQueueName = "rpc_queue_unit";
public UnitAmqpRestController(RabbitTemplate rabbitTemplate, String replyQueueName, Connection connection, Channel channel, String requestQueueName) {
this.rabbitTemplate = rabbitTemplate;
this.replyQueueName = replyQueueName;
this.connection = connection;
this.channel = channel;
this.replyQueueName = requestQueueName;
}
@PostConstruct
public static void main() throws Exception {
rabbitTemplate.setReplyTimeout(15_000L);
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(getHostProperty());
factory.setPort(mqPort);
connection = factory.newConnection();
channel = connection.createChannel();
}
public static String getHostProperty() {
Properties properties = new Properties();
try {
File file = ResourceUtils.getFile("classpath:application.properties");
InputStream in = new FileInputStream(file);
properties.load(in);
} catch (IOException e) {
}
return properties.getProperty("spring.rabbitmq.host");
}
here is the error :
2019-06-18 10:58:02.642 INFO 29037 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2019-06-18 10:58:02.661 INFO 29037 --- [ main] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 2019-06-18 10:58:02.667 ERROR 29037 --- [ main] o.s.boot.SpringApplication : Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'unitAmqpRestController': Invocation of init method failed; nested exception is java.lang.NullPointerException at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE] at com.infy.ci.unitamqpservice.UnitamqpserviceApplication.main(UnitamqpserviceApplication.java:10) [classes!/:0.0.1-SNAPSHOT] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212] at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [unitamqpservice-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [unitamqpservice-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [unitamqpservice-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [unitamqpservice-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] Caused by: java.lang.NullPointerException: null at com.infy.ci.unitamqpservice.UnitAmqpRestController.main(UnitAmqpRestController.java:69) ~[classes!/:0.0.1-SNAPSHOT] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] ... 26 common frames omitted
Not getting whether issue is in constructor OR in the main method ?? Am I missing something like @Bean configuration ??
Please suggest
@Valueon static fields. You need the rabbit template to be available for the controller to be constructed but you are creating it inside the controller. Next you are also reading the properties yourself. In short you are working against/around the framework instead of actually using it. Spring Boot already creates theConnectionFactory,RabbitTemplateetc. for you, don't do it yourself. Remove all that stuff from the controller and only leave the@controllerand@RequestMapping. There also appears to be nothing exposed in the controller. - M. Deinumspring-boot-starter-amqpas a dependency. - M. Deinum