2016-06-07 116 views
1

我想使用大氣框架的JMSBroadcaster爲了在我的應用程序的兩個實例之間傳播消息。爲此,我需要爲JMS ConnectionFactoryTopic(我們使用的實現是Tibco EMS)查找(JNDI)。使用JMS Tibco EMS JNDI資源啓動Tomcat時,Catalina出現NullPointerException

我對JMS非常新,我無法確定如何在Tomcat的context.xmlserver.xml(我沒有使用活動MQ)中配置Tibco JMS資源。我試圖從EMS在Tomcat中8 聲明這些JMS資源我tomcat8/lib/

添加tibjms-7.0.1.jarjboss-jms-api_1.1_spec-1.0.1.Final.jar但是,在Tomcat啓動,我收到以下錯誤(即使沒有任何部署WAR):

嚴重[主] org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans的RuntimeException顯示java.lang.NullPointerException

我無法弄清楚如何哪裏出了問題(我不知道我能有這個記錄的詳細信息錯誤)。

這裏是我的Tomcat配置:

server.xml

<Resource 
    id="atmosphereFactory" 
    name="jms/atmosphereFactory" 
    jndiName="atmosphereFactory" 
    auth="Container" 
    type="com.tibco.tibjms.naming.TibjmsInitialContextFactory" 
    factory="com.tibco.tibjms.naming.TibjmsObjectFactory" 
    factoryClass="com.tibco.tibjms.naming.TibjmsInitialContextFactory" 
    brokerName="localhost" 
    brokerURL="tcp://localhost:7222" 
    serverUrl="localhost:7222" 
    userName="admin" 
    password="" /> 
<Resource 
    id="atmosphere" 
    name="jms/atmosphere/test.atmo" 
    jndiName="atmosphere" 
    auth="Container" 
    type="com.tibco.tibjms.TibjmsTopic" 
    factory="com.tibco.tibjms.naming.TibjmsObjectFactory" 
    physicalName="test.atmo"/> 

context.xml

<ResourceLink 
    global="jms/atmosphereFactory" 
    name="jms/atmosphereFactory" 
    type="com.tibco.tibjms.naming.TibjmsInitialContextFactory" /> 
<ResourceLink 
    global="jms/atmosphere" 
    name="jms/atmosphere" 
    type="com.tibco.tibjms.TibjmsTopic" /> 

或者,我也可以用ConnectionFactoryTopic有興趣的方式來配置大氣的JMSBroadcaster從Spring注入。

回答

0

我實際使用Spring來實例化JMS主題...

<!-- Connection to Tibco EMS --> 
<bean id="tibjmsConnectionFactory" class="com.tibco.tibjms.TibjmsConnectionFactory"> 
    <property name="serverUrl" value="${instance.jms.server}" /> 
    <property name="userName" value="${instance.jms.login}"/> 
    <property name="userPassword" value="${instance.jms.password}"/> 
</bean> 

<bean id="jmsTemplateEms" class="org.springframework.jms.core.JmsTemplate"> 
    <property name="connectionFactory" ref="tibjmsConnectionFactory" /> 
    <property name="explicitQosEnabled" value="true" /> 
    <property name="deliveryMode" value="2" /> 
    <property name="sessionAcknowledgeModeName" value="CLIENT_ACKNOWLEDGE" /> 
    <property name="sessionTransacted" value="false" /> 
    <property name="receiveTimeout" value="${instance.wait.timeout}" /> 
</bean> 

<bean id="pushJmsMessageListener" class="com.agipi.g2a.tiana.web.utils.PushJmsMessageListener" /> 

<bean id="atmosphereTopic" class="com.tibco.tibjms.TibjmsTopic"> 
    <!-- nom du topic--> 
    <constructor-arg index="0" value="${instance.jms.atmosphere.topic.name}" /> 
</bean> 
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> 
    <property name="connectionFactory" ref="tibjmsConnectionFactory"/> 
    <property name="destination" ref="atmosphereTopic"/> 
    <property name="messageListener" ref="pushJmsMessageListener" /> 
</bean> 

...並創建了聽大氣的話題和發佈收到消息推送事件總線一個MessageListener ...

class PushJmsMessageListener implements MessageListener { 
    private static final Logger LOGGER = LoggerFactory.getLogger(PushJmsMessageListener.class); 

    private static final String PROPERTY_PUSH_CHANNEL = "pushChannel"; 

    @Override 
    public void onMessage(Message message) { 

     try { 
      EventBus eventBus = EventBusFactory.getDefault().eventBus(); 
      TextMessage testMessage = (TextMessage) message; 
      LOGGER.info("Reception d'un message du topic atmosphere : {}", testMessage); 

      String canal = testMessage.getStringProperty(PROPERTY_PUSH_CHANNEL); 
      Object decodedObject = new JSONDecoder().decode(testMessage.getText()); 
      LOGGER.info("Envoi du message sur le endpoint push [canal={},objet={}]", canal, decodedObject); 
      eventBus.publish(canal, decodedObject); 
     } catch (JMSException e) { 
      LOGGER.error("error.receiving.jms.message", e); 

     } 
    } 
} 

...並創建了一個春季服務發佈我的信息的話題,而不是推...

@Service 
@Scope(value = "singleton") 
public class JmsAtmosphereServiceImpl implements JmsAtmosphereService { 

    @Autowired 
    @Qualifier("jmsTemplateEms") 
    private JmsTemplate jmsTemplate; 

    @Autowired 
    @Qualifier("atmosphereTopic") 
    private Topic atmosphereTopic; 


    @Override 
    public void sendMessage(String pushChannel, String jsonContent) { 
     jmsTemplate.send(atmosphereTopic, session -> { 
      TextMessage textMessage = session.createTextMessage(jsonContent); 
      textMessage.setStringProperty("pushChannel", pushChannel); 
      return textMessage; 
     }); 

    } 

} 

...一些工具來抽象...

@Service 
public class PushJmsUtils { 

    private static final String PUSH_DEFAULT_CHANNEL = "atmosphere"; 

    @Autowired 
    private JmsAtmosphereService jmsAtmosphereService; 

    /** 
    * Propagate message JMS as JSON to JMS Atmosphere topic. 
    * 
    * @param channel push channel 
    * @param message object to send via push 
    */ 
    public void propagateMessage(String channel, Object message) { 
     String id = channel; 
     if (id.startsWith("/*")) { 
      id = PUSH_DEFAULT_CHANNEL; 
     } 
     jmsAtmosphereService.sendMessage(id, new JSONEncoder().encode(message)); 
    } 

} 

..然後,我出版我的消息,聽着同樣的氣氛主題(包括應用程序發送的消息),我的應用程序的多個實例。

pushJmsUtils.propagateMessage(canal,pushMessage);