如题代码如下:
public class MsgConsumer implements MessageListener { private static final Logger LOGGER = LoggerFactory.getLogger(MsgConsumer.class); private static final String BROKER_URL = ActiveMQConnection.DEFAULT_BROKER_URL; private static final String SUBJECT = "test-activemq-queue"; public static void main(String[] args) throws JMSException { //初始化ConnectionFactory ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(BROKER_URL); //创建mq连接 Connection conn = connectionFactory.createConnection(); //启动连接 conn.start(); //创建会话 Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); //通过会话创建目标 Destination dest = session.createQueue(SUBJECT); //创建mq消息的消费者 MessageConsumer consumer = session.createConsumer(dest); //初始化MessageListener MsgConsumer me = new MsgConsumer(); //给消费者设定监听对象 consumer.setMessageListener(me); } @Override public void onMessage(Message message) { TextMessage txtMessage = (TextMessage)message; try { LOGGER.info ("get message: " + txtMessage.getText()); String oldId = txtMessage.getText(); //这里查询就报错了..... MostViewed model = new MostViewed().findFirst("select * from most_viewed where old_id= ?", oldId); model.setPlayImgSrc("***"); model.setPlayUrl("***"); boolean result = model.update(); LOGGER.info("MQ执行修改数据ID={}结果为:{}",oldId,result); } catch (JMSException e) { LOGGER.error("error {}", e); } } }
今天在学习测试一个爬虫列子,用到了activeMQ和jfinal,一切工作准备就绪后,往第三方网站开始爬数据然后得到一个list,然后循环正常入库并循环创建一个消息队列在MQ中,这时候当MQ的消费者端接收消息时(MQ中的消费者端我实现了MessageListener接口,并重写了onMessage方法),根据接收到的消息进行爬数据采集更新数据库;这时候就出现错误了,错误日志如下:
java.lang.NullPointerException at com.jfinal.plugin.activerecord.Model.find(Model.java:588) at com.jfinal.plugin.activerecord.Model.findFirst(Model.java:606) at com.softisland.common.activemq.MsgConsumer.getDetail(MsgConsumer.java:75) at com.softisland.common.activemq.MsgConsumer.onMessage(MsgConsumer.java:58) at org.apache.activemq.ActiveMQMessageConsumer.dispatch(ActiveMQMessageConsumer.java:1404) at org.apache.activemq.ActiveMQSessionExecutor.dispatch(ActiveMQSessionExecutor.java:131) at org.apache.activemq.ActiveMQSessionExecutor.iterate(ActiveMQSessionExecutor.java:202) at org.apache.activemq.thread.PooledTaskRunner.runTask(PooledTaskRunner.java:133) at org.apache.activemq.thread.PooledTaskRunner$1.run(PooledTaskRunner.java:48) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)
是不是程序访问数据库不经过jfinal的路由器就得不到数据源啊?因为MQ监听到消息后执行数据库就报空指针了;求波总&&高手们给个解决思路@jFinal