我想连接到一个Sonic Broker主题并侦听任何传入的XML消息。我做了下面这样的事情;
application.java
@SpringBootApplication
@ComponentScan({"com.mainpack", "com.msgpack.jms"})
@EnableJms
public class Application extends SpringBootServletInitializer {
@Autowired
private JmsTopicListener jmsTopicListener;
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
try {
LogService.info(Application.class.getName(), "Starting Service...");
super.onStartup(servletContext);
jmsTopicListener.listenMessage();
LogService.info(Application.class.getName(), "Service Started");
} catch (Exception ex) {
LogService.error(this.getClass().getName(), ex);
}
}
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(Application.class, args);
LogService.info(Application.class.getName(), "Service Started...");
}
}
jmstopicListener.java
@Component
public class JmsTopicListener {
@Autowired
private ApplicationProperties properties;
@Autowired
private MsgListener msgListener;
public void listenMessage() {
TopicConnectionFactory factory;
TopicConnection connection = null;
LogService.info(this.getClass().getName(), "Registering Broker Connection");
try {
factory = new progress.message.jclient.TopicConnectionFactory(properties.getBrokerURL());
connection = factory.createTopicConnection(properties.getUserName(), properties.getPass());
javax.jms.TopicSession subSession = (TopicSession) connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
javax.jms.Topic topic = subSession.createTopic(properties.getTopicName());
MessageConsumer subscriber = subSession.createSubscriber(topic);
subscriber.setMessageListener(msgListener);
connection.start();
LogService.info(this.getClass().getName(), "Broker connected");
} catch (Exception ex) {
LogService.error(this.getClass().getName(), ex);
}
}
}
msgListener.java
@Component
public class MsgListener implements MessageListener {
@Override
public void onMessage(Message msg) {
if (msg instanceof XMLMessage) {
try {
XMLMessage m = (XMLMessage) msg;
if (m.getText().contains("Applications")) {
LogService.info(this.getClass().getName(), "Recieved A Applications Message");
} else {
LogService.info(this.getClass().getName(), "Recieved Message Does not contain Applications Tag");
}
} catch (Exception ex) {
LogService.info(this.getClass().getName(), "Exception: " + ex.getMessage());
}
}
}
}
记录器出错:
ERROR [2015-07-14 14:34:52] [com.mainpack.Application] [localhost-startStop-1] - [Exception: ]java.lang.NullPointerException
at com.mainpack.Application.onStartup(Application.java:33)
at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5156)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:945)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1768)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
onstartup
在应用程序生命周期的早期由servlet容器调用,并在servlet容器创建的类的实例上调用,而不是Spring Boot。这就是jmstopiclistener
为null
的原因。
您可以使用带有@postconstruct
注释的方法,而不是重写onstartup
。Spring将在创建application
实例并注入任何依赖项后调用它:
@SpringBootApplication
@ComponentScan({"com.mainpack", "com.msgpack.jms"})
@EnableJms
public class Application extends SpringBootServletInitializer {
@Autowired
private JmsTopicListener jmsTopicListener;
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
@PostConstruct
public void listen() {
jmsTopicListener.listenMessage();
}
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(Application.class, args);
LogService.info(Application.class.getName(), "Service Started...");
}
}
当我在eclipse中运行spring boot java项目时,我得到了以下错误。错误:无法找到或加载主类com。埃森哲。艾科。导入数据。批处理应用程序
org.springframework.beans.factory.beanCreationException:创建名为“birthday controller”的bean时出错:注入autowired依赖项失败;嵌套异常为org.springframework.beans.factory.beanCreationException:无法自动连接字段:private com.esri.birthd
最近我学到了很多关于Spring的知识,我想我可能会误解的一件事是@Autowired注释,尤其是在构造函数中使用它时。你看,我正在开发的应用程序是一个服务,所以基本上所有东西都是在构造函数中初始化的。唯一实际发生的用户驱动事件是重启服务某些模块的按钮。这是我的主要方法: 这是我的主类的构造函数,基本上所有事情都发生在这里。我省略了对初始化每个模块的方法的调用,以缩短它: 我的主类为每个服务都有一
设置好JPA实体后,Spring Boot接口似乎会很好地帮助我。在我的情况下,我希望根据需要创建这些CrudRepository实例,而不是总是基于@注释。例如,在我的用例中,我有一个GUI菜单,它将包含一个域(或引用)表名列表。根据用户选择的内容,程序将实例化这些CrudRepository实现之一。 如果我使用,那么我不是必须在代码中使用所有可能的已知实现吗? 一个假设用例: (我才意识到我
我有一个用@service注释的类(HttpHandler)。我正在为这个服务类编写单元测试用例。我在测试类中使用@Autowired注释来获取服务的对象。但是,当我运行单元测试时,它返回NullPointerException。有人能帮帮我吗? 下面是服务类的代码:
我只是用kotlin和spring Boot实现quartz调度器。