Starting ActiveMQ message listener in Tomcat? - tomcat6

I've created an ActiveMQ MessageListener and configured it using Spring. I'm hosting the listener in Tomcat. When I start up the web application (that features only the listener), should the listener automatically start? Or do I need additional configuration?
Here's what I have. First, updated the web.xml to allow spring to configure itself on startup,
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/applicationContext.xml</param-value>
</context-param>
</web-app>
Then I created the applicationContext.xml to configure the ActiveMQ listener,
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:amq="http://activemq.apache.org/schema/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.somepackage"/>
<context:property-placeholder location="classpath:env.properties"/>
<bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616" />
</bean>
<bean id="documentListener" class="com.somepackage.SomeListener" />
<bean id="container"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="cachingConnectionFactory"/>
<property name="messageListener" ref="documentListener"/>
<property name="destinationName" value="STOCKS.MSFT" />
</bean>
<bean id="cachingConnectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="jmsFactory" />
<property name="sessionCacheSize" value="1" />
</bean>
</beans>
And that's it. Based on what I've seen around the web, I can't tell if that's all I need? Maybe I need some other configuration in Tomcat to kick off the listener?

This was actually working. I was debugging this using Eclipse and I had to do a couple refreshes, cleans and restarts before it suddenly decided to work.
To answer my own question, yes, this is all that is required in order to run a listener in a web application from within Tomcat. It can be totally configured with XML only.

Related

Camel route on Wildfly Swarm trying to load h2 database when it shouldn't

I have a Camel route running on Wildfly Swarm (2017.8.1) that connects to a SQL Server database using JTDS driver. However when I try and run it, it tries to load the h2 database module. I'm not using h2.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>app.properties</value>
</property>
</bean>
<bean id="coreDS" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
<property name="url" value="${db.core.uri}"/>
<property name="username" value="${db.core.user}"/>
<property name="password" value="${db.core.password}"/>
</bean>
<bean id="sql" class="org.apache.camel.component.sql.SqlComponent">
<property name="dataSource" ref="coreDS"/>
</bean>
<camelContext xmlns="http://camel.apache.org/schema/spring">
<propertyPlaceholder id="placeholder" location="classpath:app.properties"/>
<route id="retrieveZipFile">
<from uri="{{ftp.silica.uri}}"/>
<log message="checking for files"/>
<to uri="file://Data/temp/zip"/>
<to uri="sql:insert into dbo.Source_In(date, sourceInSystemId, filename) values (getdate(), 1, 'test');dataSource=coreDS"/>
</route>
</camelContext>
</beans>
Error:
Caused by: org.jboss.modules.ModuleLoadError: com.h2database.h2:main
at org.jboss.modules.ModuleLoadException.toError(ModuleLoadException.java:74)
at org.jboss.modules.Module.getPathsUnchecked(Module.java:1435)
at org.jboss.modules.Module.loadModuleClass(Module.java:601)
at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:190)
Why is the h2 module required? I've tried adding it to my POM bt it still fails.

Setup independent ActiveMQ broker in camel blueprint xml

I am setting up a ServieMix instance using apache-camel as the routing engine, with my routes defined in a blueprint.xml. I am trying to configure ActiveMQ for my blueprint to be completely isolated from anything else (use its own, private, broker).
Here is my camel blueprint XML
<?xml version="1.0" encoding="UTF-8"?>
<blueprint
xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
>
<bean id="record_ip" class="my.service.RecordIP"/>
<bean id="jmsConnectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="vm://myBroker?create=true&waitForStart=10000" />
<property name="userName" value="shadow"/>
<property name="password" value="broker"/>
</bean>
<bean id="pooledConnectionFactory"
class="org.apache.activemq.pool.PooledConnectionFactory" init-method="start" destroy-method="stop">
<property name="maxConnections" value="8" />
<property name="connectionFactory" ref="jmsConnectionFactory" />
</bean>
<bean id="jmsConfig"
class="org.apache.camel.component.jms.JmsConfiguration">
<property name="connectionFactory" ref="pooledConnectionFactory"/>
<property name="concurrentConsumers" value="10"/>
</bean>
<bean id="activemq"
class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="configuration" ref="jmsConfig"/>
</bean>
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<route id="tracing_route">
<from uri="jetty:http://localhost:9696/trace"/>
<inOnly uri="activemq:queue:ip_capture"/>
</route>
<route id="ip_capture">
<from uri="activemq:queue:ip_capture?concurrentConsumers=1&maxConcurrentConsumers=64&maxMessagesPerTask=100"/>
<bean ref="record_ip"/>
<log message="Finished!" loggingLevel="WARN" />
</route>
</camelContext>
</blueprint>
I don't think it's using the setup at all because I get the following error
Could not refresh JMS Connection for destination 'ip_capture' - retrying in 5000 ms. Cause: Error while attempting to add new Connection to the pool; nested exception is javax.jms.JMSException: Could not create Transport. Reason: java.io.IOException: Broker named 'amq-broker' does not exist.
And amq-broker is the default broker.
I'm pouring through everything I can find but something important is missing
http://camel.apache.org/activemq.html
http://activemq.apache.org/networks-of-brokers.html
http://activemq.apache.org/how-do-i-embed-a-broker-inside-a-connection.html
http://activemq.apache.org/xml-configuration.html
I can't use xmlns:amq="http://activemq.apache.org/schema/core" (ServiceMix can't resolve it)
I am using
ServiceMix 7.0.0
apache-camel/camel-blueprint 2.16.4
activemq-client/camel/blueprint 5.14.3
So long story sort, How do I properly configure ActiveMQ for my blueprint to be completely isolated from anything else?
I ended up not really needing to do this, because for all intents and purposes, the broker is independent, and setting up a new one doesn't change anything.

Problems With Activiti OSGI and Blueprint

i try to use Activiti in KARAF OSGI with camel.
I copied some parts out of the service mix configuration.
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/blueprint"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd"
xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.0.0"
xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.0.0"
xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0">
<ext:property-placeholder />
<!--
Setting up the process engine configuration, using an embedded H2 database together with our default Aries
transaction manager.
-->
<bean id="dataSource" class="org.h2.jdbcx.JdbcDataSource">
<property name="URL" value="jdbc:h2:~/activiti"/>
<property name="user" value="sa"/>
<property name="password" value=""/>
</bean>
<reference id="transactionManager" interface="javax.transaction.TransactionManager"/>
<bean id="configuration" class="org.activiti.engine.impl.cfg.JtaProcessEngineConfiguration" ext:field-injection="true">
<property name="databaseType" value="h2"/>
<property name="dataSource" ref="dataSource"/>
<property name="transactionManager" ref="transactionManager"/>
<property name="databaseSchemaUpdate" value="true"/>
<property name="transactionsExternallyManaged" value="true" />
<property name="defaultCamelContext" value="defaultContext"/>
</bean>
<!--
Set up the custom resolver implementation to ease integration with Camel routes
-->
<bean id="resolver" class="de.myproject.CamelAwareELResolver"/>
<reference-list availability="optional" interface="org.activiti.camel.ContextProvider">
<reference-listener ref="resolver" bind-method="addContextProvider" unbind-method="removeContextProvider" />
</reference-list>
<reference-list availability="optional" interface="org.activiti.engine.delegate.JavaDelegate">
<reference-listener ref="resolver" bind-method="bindService" unbind-method="unbindService" />
</reference-list>
<!--
Set up the Activiti process engine itself
-->
<bean id="processEngineFactory" class="org.activiti.osgi.blueprint.ProcessEngineFactoryWithELResolver" init-method="init" destroy-method="destroy">
<property name="processEngineConfiguration" ref="configuration"/>
<property name="bundle" ref="blueprintBundle"/>
<property name="blueprintELResolver" ref="resolver" />
</bean>
<bean id="processEngine" factory-ref="processEngineFactory" factory-method="getObject"/>
<bean id="runtimeService" factory-ref="processEngine" factory-method="getRuntimeService" />
<!--
Register the ProcessEngine and RuntimeService as OSGi services to allow other bundles to use them
-->
<service ref="processEngine" interface="org.activiti.engine.ProcessEngine"/>
<service ref="runtimeService" interface="org.activiti.engine.RuntimeService"/>
<bean id="routeBuilder" class="de.myproject.bpmn.BpmnRestRouteBuilder"/>
<camelContext id="clientContext" trace="false" xmlns="http://camel.apache.org/schema/blueprint">
<routeBuilder ref="routeBuilder" />
<route id="entryPointClient">
<from uri="vm:client_queue"/>
<to uri="jobsbpmn://bpmn"/>
</route>
</camelContext>
I can deploy the bundle without problems in the container. When i trigger a workflow with a camel module i get those exception:
2016-11-08 11:56:43,569 | ERROR | m://client_queue | JobsBpmnProducer
| 45 - jobs-logging - 1.0.0 | No Processdefinition found with this
Identifier ActivitiException: Expecting a
SpringProcessEngineConfiguration for the Activiti Camel module.
ClassCastException:
org.activiti.engine.impl.cfg.JtaProcessEngineConfiguration cannot be
cast to org.activiti.spring.SpringProcessEngineConfiguration**
How can i deploy a OSGI conform Processengine using spring to get Camel to work?
This problem might be fixed in the Activity code already.
See https://github.com/Activiti/Activiti/pull/519
Can you try with the newest Activitiy version?

Database Configuration In Spring As Well As Hibernate config files

I am a newbie to Spring and Hibernate.And while browsing through projects, I found database details like connection-url, username, password in both hibernate and spring xml config files.
I need to understand why we do so ?
Yes. If you using both Hibernate and Spring then you can also try like that.
You need both hibernate.cfg.xml for hibernate and ApplicationContext.xml for spring.
do like this, at first create your hibernate.cfg.xml.
i.e
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/country</property>
<property name="connection.username">root</property>
<property name="connection.password">password</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">false</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="com.hibernate.test" />
</session-factory>
</hibernate-configuration>
now create your ApplictionContext.xml,and add your hibernate.cfg.xml as properties in sessionFactory bean.
i.e
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
//package to scan for Annotated classes
<context:component-scan base-package="com.hibernate.spring" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
//add and locate the hibernate.cfg.xml here
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
</beans>
by this you only need to call Application.xml,the hibernate.cfg.xml will automatically loaded.
You have two options.
You can have hibernate to manage your database connections or you can have Spring managing your connections.
When you have Hibernate managing your connections, you only need to tell Spring where hibernate configuration is. Sorry, I couldn't find very easily an example where Hibernate manages the connections and Spring only "using" hibernate.
The other option is to use Spring to manage your connections and hbm files/annotated entities.
Here is a snippet from Spring docs
<beans>
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:hsql://localhost:9001"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="mappingResources">
<list>
<value>product.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.HSQLDialect
</value>
</property>
</bean>
</beans>
and here the full documentation
http://docs.spring.io/spring/docs/4.0.5.RELEASE/spring-framework-reference/htmlsingle/#orm-hibernate
Besides very specific problems, I can't a lot of differences on using either way but I would prefer to let Spring to manage my database connections and I can let Spring to deal transactions for example and have everything centralized in the same configuration file.

DatastoreJDOPersistenceManagerFactory class not found exception when configuring JDO on Appengine

I am trying to configure JDO on Appengine but every time I try to persist something I get an
java.lang.ClassNotFoundException: org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManagerFactory
Searching for this error I only got that I could have an error in my jdoconfig.xml
But I explicitly declare that I want to use this class my jdoconfig.xml, which I posted below.
<?xml version="1.0" encoding="utf-8"?>
<jdoconfig xmlns="http://java.sun.com/xml/ns/jdo/jdoconfig"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig">
<persistence-manager-factory name="transactions-optional">
<property name="javax.jdo.PersistenceManagerFactoryClass"
value="org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManagerFactory"/>
<property name="javax.jdo.option.ConnectionURL" value="appengine"/>
<property name="javax.jdo.option.NontransactionalRead" value="true"/>
<property name="javax.jdo.option.NontransactionalWrite" value="true"/>
<property name="javax.jdo.option.RetainValues" value="true"/>
<property name="datanucleus.appengine.autoCreateDatastoreTxns" value="true"/>
</persistence-manager-factory>
</jdoconfig>
decide if you are using GAE JDO plugin v1.0 (JDO2.x) or GAE JDO plugin v2.0 (JDO3.0) which kinda decides which jars you have in the classpath, and hence what entries you put in your jdoconfig.xml

Resources