I am using Spring Boot to send and receive messages to/from an ActiveMQ topic, but when receiving and try to store the received message using an @Autowired
object it throws a NullPointerException
.
java.lang.NullPointerException: null
at com.example.demo.Subscriber.Subcriber.onMessage(Subcriber.java:56) ~[classes/:na]
at org.apache.activemq.ActiveMQMessageConsumer.dispatch(ActiveMQMessageConsumer.java:1404) [activemq-client-5.15.13.jar:5.15.13]
at org.apache.activemq.ActiveMQMessageConsumer.iterate(ActiveMQMessageConsumer.java:1575) [activemq-client-5.15.13.jar:5.15.13]
at org.apache.activemq.ActiveMQSessionExecutor.iterate(ActiveMQSessionExecutor.java:191) [activemq-client-5.15.13.jar:5.15.13]
at org.apache.activemq.thread.PooledTaskRunner.runTask(PooledTaskRunner.java:133) [activemq-client-5.15.13.jar:5.15.13]
at org.apache.activemq.thread.PooledTaskRunner$1.run(PooledTaskRunner.java:48) [activemq-client-5.15.13.jar:5.15.13]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_181]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]}
@Configuration
@EnableJms
public class ConnectionFactoryConfig {
@Value("${spring.activemq.broker-url}")
String brokerUrl;
@Value("${spring.activemq.user}")
String userName;
@Value("${spring.activemq.password}")
String password;
/*
* Initial ConnectionFactory
*/
public static Boolean TRANSACTIONAL = false;
private static final String MASSAGE_TOPIC = "book";
@Bean
public ConnectionFactory connectionFactory() throws JMSException {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL(brokerUrl);
connectionFactory.setUserName(userName);
connectionFactory.setPassword(password);
Connection connection = connectionFactory.createConnection();
connection.setClientID("boo");
connection.start();
RedeliveryPolicy topicPolicy = new RedeliveryPolicy();
topicPolicy.setInitialRedeliveryDelay(0);
topicPolicy.setRedeliveryDelay(1000);
topicPolicy.setUseExponentialBackOff(false);
topicPolicy.setMaximumRedeliveries(0);
connectionFactory.setRedeliveryPolicy(topicPolicy);
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic destination = session.createTopic(MASSAGE_TOPIC);
MessageConsumer consumer = session.createDurableSubscriber(destination, "Listener");
consumer.setMessageListener(new Subcriber());
return connectionFactory;
}
@Component
public class Subcriber implements MessageListener {
@Autowired
private BookStoreService bookStoreService;
@Autowired
private BookStoreBooksRepo bookStoreBooksRepo;
@Autowired
private BookStoreRepo2 bookStoreRepo;
BookStoreBooks bs=null;
@Override
public void onMessage(javax.jms.Message message) {
if (message instanceof TextMessage) {
String s;
try {
s = ((TextMessage) message).getText();
InputStream targetStream = new ByteArrayInputStream(s.getBytes());
JAXBContext context = JAXBContext.newInstance(BookXml.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
BookXml book = (BookXml) unmarshaller.unmarshal(targetStream);
System.out.println(book.toString());
Bookstore bs1 = new Bookstore(6, "Amazon", "London");
bs = new BookStoreBooks();
bs.setSid(bs1);
bs.setBid(book.getId());
bs.setBn(book.getName());
System.out.println(bs.getBn());
//null pointer "trying to save object to DB using @Repository
bookStoreBooksRepo.save(bs);
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
throw new IllegalArgumentException("Message must be of type TextMessage");
}
}