ActiveMQ maven plugin configuration - spring

I have a problem with using ActiveMQ in a Spring project.
I am trying to integrate the ActiveMQ maven plugin into my project to use it in integration tests.
Here my configuration:
<?xml version="1.0" encoding="UTF-8"?> <!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--> <!-- START SNIPPET: example --> <beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:cam ="http://camel.apache.org/schema/spring"
xmlns:jetty ="http://mortbay.com/schemas/jetty/1.0"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.5.0.xsd
http://mortbay.com/schemas/jetty/1.0
http://jetty.mortbay.org/jetty.xsd"> <!-- this location for the schema doesn't work, I dont know exactly where the schema is located
xmlns:jetty ="http://mortbay.com/schemas/jetty/1.0"
http://mortbay.com/schemas/jetty/1.0
http://jetty.mortbay.org/jetty.xsd -->
<!-- Allows us to use system properties as variables in this configuration file -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>file:///${activemq.base}/conf/credentials.properties</value>
</property>
</bean>
<amq:broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.base}/data">
<!-- Destination specific policies using destination names or wildcards -->
<destinationPolicy>
<policyMap>
<policyEntries>
<policyEntry queue=">" memoryLimit="5mb"/>
<policyEntry topic=">" memoryLimit="5mb">
<!-- you can add other policies too such as these
<dispatchPolicy>
<strictOrderDispatchPolicy/>
</dispatchPolicy>
<subscriptionRecoveryPolicy>
<lastImageSubscriptionRecoveryPolicy/>
</subscriptionRecoveryPolicy>
-->
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
<!-- Use the following to configure how ActiveMQ is exposed in JMX -->
<managementContext>
<managementContext createConnector="false"/>
</managementContext>
<!-- The store and forward broker networks ActiveMQ will listen to -->
<networkConnectors>
<!-- by default just auto discover the other brokers -->
<networkConnector name="default-nc" uri="multicast://default"/>
<!-- Example of a static configuration:
<networkConnector name="host1 and host2" uri="static://(tcp://host1:61616,tcp://host2:61616)"/>
-->
</networkConnectors>
<persistenceAdapter>
<amqPersistenceAdapter syncOnWrite="false" directory="${activemq.base}/data" maxFileLength="20 mb"/>
</persistenceAdapter>
<!-- Use the following if you wish to configure the journal with JDBC -->
<!--
<persistenceAdapter>
<journaledJDBC dataDirectory="${activemq.base}/data" dataSource="#postgres-ds"/>
</persistenceAdapter>
-->
<!-- Or if you want to use pure JDBC without a journal -->
<!--
<persistenceAdapter>
<jdbcPersistenceAdapter dataSource="#postgres-ds"/>
</persistenceAdapter>
-->
<sslContext>
<sslContext keyStore="file:${activemq.base}/conf/broker.ks" keyStorePassword="password" trustStore="file:${activemq.base}/conf/broker.ts" trustStorePassword="password"/>
</sslContext>
<!-- The maximum about of space the broker will use before slowing down producers -->
<systemUsage>
<systemUsage>
<memoryUsage>
<memoryUsage limit="20 mb"/>
</memoryUsage>
<storeUsage>
<storeUsage limit="1 gb" name="foo"/>
</storeUsage>
<tempUsage>
<tempUsage limit="100 mb"/>
</tempUsage>
</systemUsage>
</systemUsage>
<!-- The transport connectors ActiveMQ will listen to -->
<transportConnectors>
<!--<transportConnector name="openwire" uri="tcp://localhost:61616" discoveryUri="multicast://default"/>-->
<!--<transportConnector name="default-nc" uri="multicast://default"/>-->
<transportConnector name="openwire" uri="tcp://localhost:61616" />
<transportConnector name="ssl" uri="ssl://localhost:61617"/>
<transportConnector name="stomp" uri="stomp://localhost:61613"/>
<transportConnector name="xmpp" uri="xmpp://localhost:61222"/>
</transportConnectors>
</amq:broker>
<!--
** Lets deploy some Enterprise Integration Patterns inside the ActiveMQ Message Broker
** For more details see
**
** http://activemq.apache.org/enterprise-integration-patterns.html
-->
<cam:camelContext id="camel">
<!-- You can use a <package> element for each root package to search for Java routes -->
<cam:package>org.foo.bar</cam:package>
<!-- You can use Spring XML syntax to define the routes here using the <route> element -->
<cam:route>
<cam:from uri="activemq:example.A"/>
<cam:to uri="activemq:example.B"/>
</cam:route>
</cam:camelContext>
<!--
** Lets configure some Camel endpoints
**
** http://activemq.apache.org/camel/components.html
-->
<!-- configure the camel activemq component to use the current broker -->
<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent" >
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="vm://localhost?create=false&waitForStart=10000" />
<property name="userName" value="${activemq.username}"/>
<property name="password" value="${activemq.password}"/>
</bean>
</property>
</bean>
<!-- Uncomment to create a command agent to respond to message based admin commands on the ActiveMQ.Agent topic -->
<!--
<commandAgent xmlns="http://activemq.apache.org/schema/core" brokerUrl="vm://localhost" username="${activemq.username}" password="${activemq.password}"/>
-->
<!-- An embedded servlet engine for serving up the Admin console -->
<jetty:jetty>
<connectors>
<nioConnector port="8161"/>
</connectors>
<handlers>
<webAppContext contextPath="/admin" resourceBase="${activemq.base}/webapps/admin" logUrlOnStart="true"/>
<webAppContext contextPath="/demo" resourceBase="${activemq.base}/webapps/demo" logUrlOnStart="true"/>
<webAppContext contextPath="/fileserver" resourceBase="${activemq.base}/webapps/fileserver" logUrlOnStart="true"/>
</handlers>
</jetty:jetty>
<!-- This xbean configuration file supports all the standard spring xml configuration options -->
</beans>
The problem I have is using the jetty Namespace.
The schema cannot be found and downloaded: http://jetty.mortbay.org/jetty.xsd
Here a link from Apache ActiveMQ:
http://activemq.apache.org/complex-single-broker-configuration-stomp-only.html
There is not specified any location for this schema.
The ActiveMQ starts without having the schema location but if I use a schema validator like in Eclipse it tells me that I have an error in the file and the schema location cannot be found.
Any idea where I can find the schema for the jetty element ?

I ran into this problem too and I lost a whole hour to find a solution.
Essentially the example in the documentation is outdated.
The dependencies required to enable jetty with activemq-maven-plugin are as follows:
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-xbean</artifactId>
<version>6.1.25</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all-server</artifactId>
<version>7.6.7.v20120910</version>
</dependency>
Following is the complete configuration of the activemq-maven-plugin:
<plugin>
<groupId>org.apache.activemq.tooling</groupId>
<artifactId>activemq-maven-plugin</artifactId>
<version>5.8.0</version>
<configuration>
<configUri>${configUri}</configUri>
<fork>false</fork>
<systemProperties>
<property>
<name>javax.net.ssl.keyStorePassword</name>
<value>password</value>
</property>
<property>
<name>org.apache.activemq.default.directory.prefix</name>
<value>./target/</value>
</property>
</systemProperties>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-spring</artifactId>
<version>5.8.0</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-xbean</artifactId>
<version>6.1.25</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all-server</artifactId>
<version>7.6.7.v20120910</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-leveldb-store</artifactId>
<version>5.8.0</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>start-activemq</id>
<goals>
<goal>run</goal>
</goals>
<phase>pre-integration-test</phase>
</execution>
</executions>
</plugin>
Then to enable jetty is enough to import the jetty configuration file into the activemq the configuration file.
The following snippet is taken from the file ${ACTIVEMQ_HOME}/conf/activemq.xml in the ActiveMQ latest release at the time of this writing (5.8.0):
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:amq="http://activemq.apache.org/schema/core"
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.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
<!--
The <broker> element is used to configure the ActiveMQ broker.
-->
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}">
...
</broker>
<!--
Enable web consoles, REST and Ajax APIs and demos
Take a look at ${ACTIVEMQ_HOME}/conf/jetty.xml for more details
-->
<import resource="jetty.xml"/>
</beans>
Cheers,
Domenico

Related

activemq endpoint in spring boot ClassNotFoundException

I'm trying to move to spring boot to get cleaner dependencies in an application using Camel and ActiveMQ. I used to have activemq endpoint configurated in an XML file:
<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:jms="http://www.springframework.org/schema/jms"
xmlns:amq="http://activemq.apache.org/schema/core"
xsi:schemaLocation="http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.5.0.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<context:property-placeholder location="classpath:config.properties"/>
<bean id="jmsConnectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="${gatewaybroker}"/>
</bean>
<bean id="pooledJmsConnectionFactory" 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="pooledJmsConnectionFactory"/>
<property name="concurrentConsumers" value="10"/>
</bean>
<!-- create a Camel ActiveMQ component -->
<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="configuration" ref="jmsConfig"/>
<!-- if we are using transacted then enable CACHE_CONSUMER (if not using XA) to run faster
see more details at: http://camel.apache.org/jms
<property name="transacted" value="true"/>
<property name="cacheLevelName" value="CACHE_CONSUMER" />
-->
</bean>
</beans>
and then have routes being called using that ActiveMQ endpoint:
from("activemq:queue:configuratelearningmodule")
.routeId(routeid)
.autoStartup(false)
.process(
new Processor(){
public void process(Exchange exchange) throws Exception {
// etc....
Now I created a spring-boot app and trying to migrate progressively. I didn't find a way to get access to an endpoint "activemq" with the autoconfigured spring-boot activemq so I tried to keep my xml configuration file. After all, I'm ok to configure myself the activemq endpoint and I am only using spring-boot to not bother about versions conflicts between all elements (jpa/hibernate, spring, camel, activemq, mqtt, etc...)
However I receive this message:
An exception occured while executing the Java class.
Error creating bean with name 'dataSourceInitializerPostProcessor':
Unsatisfied dependency expressed through field 'beanFactory';
nested exception is org.springframework.beans.factory.CannotLoadBeanClassException:
Error loading class [org.apache.activemq.camel.component.ActiveMQComponent]
for bean with name 'activemq' defined in class path resource [jmsconfig.xml]:
problem with class file or dependent class;
nested exception is java.lang.NoClassDefFoundError: org/apache/camel/impl/HeaderFilterStrategyComponent
On Camel 3.1 migration page, they tell activemq-camel has been moved to camel so I'm tweaking my pom.xml with camel-activemq component but that doesn't change anything.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-camel</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-activemq</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>3.1.0</version>
</dependency>
Any idea how to get that activemq endpoint working in spring-boot ?
you could try to use
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.2</version>
</dependency>
Greets

Simple Spring project - HTTP 404

When I run the program on a tomcat server, it shows me the index page but whenever I'm trying to request an URL matching the #RequestMapping annotation, I'm getting a 404 error.
There is also no exception thrown. I put a System.out.print inside the controller's method to see if it's executed on request but there is nothing printed on the console.
It may be a configuration problem in the IDE, but despite days of searching a solution, I didn't find a way to solve it. I hope someone will help me for this issue. I'm using intellij.
Project
WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
WEB-INF/dispatcher-servlet.xml
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Add support for component scanning -->
<context:component-scan base-package="com.luv2code.springdemo" />
<!-- Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Define Spring MVC view resolver-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Step 1: Define Database DataSource / connection pool -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&serverTimezone=UTC" />
<property name="user" value="springstudent" />
<property name="password" value="springstudent" />
<!-- these are connection pool properties for C3P0 -->
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="30000" />
</bean>
<!-- Step 2: Setup Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan" value="com.luv2code.springdemo.entity" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Step 3: Setup Hibernate transaction manager -->
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- Step 4: Enable configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="myTransactionManager" />
</beans>
CustomerController
#Controller
public class CustomerController {
#RequestMapping("/list")
public String listCustomers(){
System.out.println("ici");
return "list-customers";
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>untitled1</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<springframework.version>5.1.5.RELEASE</springframework.version>
<hibernate.version>5.4.1.Final</hibernate.version>
<mysql.connector.version>5.1.47</mysql.connector.version>
<c3po.version>0.9.5.2</c3po.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.version}</version>
</dependency>
<!-- C3PO -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- Servlet+JSP+JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- to compensate for java 9+ not including jaxb -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
<build>
<finalName>spring-web-customer-tracker</finalName>
<plugins>
<!-- Builds a Web Application Archive (WAR) file from the project output
and its dependencies. -->
<plugin>
<!-- Add Maven coordinates (GAV) for: maven-war-plugin -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
</plugin>
</plugins>
</build>
</project>
There are multiple issues with the attached project like the duplicate libraries from lib directory and from Maven and the exploded web artifact not including any of the libraries. I'd recommend deleting .idea and lib directories from the project. Then reimport it from pom.xml. You should get the dependencies and the artifact configured automatically.
The main issue right now is that artifact is not deploying any jars from the dependencies, therefore none of the Spring code works and you just get the basic JSP functionality.
There is also a problem with JDBC configuration, bit it's not related to IntelliJ IDEA and you may want to submit a new question if you can't figure it out.

What is the latest (2019) preferred way to integrate spring batch with kafka?

This is a followup question to I need current easy to follow instructions for configuring spring integration kafka from XML
Spring-integration-kafka has evolved a lot over the past few iterations, and many old examples no longer function.
In particular, this bean which bridges from the spring-batch world to the spring integration world will not instantiate, because the KafkaTemplate class does not implement MessagingTemplate . What is the current recommended way to accomplish this integration?
<bean id="partitionHandler" class="org.springframework.batch.integration.partition.MessageChannelPartitionHandler">
<property name="stepName" value="fm-step0002.messager"/>
<property name="gridSize" value="3"/>
<property name="messagingOperations" ref="kafkaTemplate"/>
</bean>
Here is a snip from my POM that shows the versions of the libraries I am using:
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-kafka</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-file</artifactId>
<version>5.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>4.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-infrastructure</artifactId>
<version>4.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-integration</artifactId>
<version>4.1.1.RELEASE</version>
</dependency>
MessagingTemplate is a core Spring Integration component; it has nothing to do with the broker you are using (RabbitMQ, Kafka, JMS etc.).
You configure the template with a default channel (which is the input channel to the kafka outbound endpoint).
See the batch documentation (click the XML button at the top of the document to change the examples from java to XML).
The example there applies to JMS, but the configuration is similar for Kafka.
<bean id="partitionHandler"
class="org.springframework.batch.integration.partition.MessageChannelPartitionHandler">
<property name="stepName" value="step1"/>
<property name="gridSize" value="3"/>
<property name="replyChannel" ref="outbound-replies"/>
<property name="messagingOperations">
<bean class="org.springframework.integration.core.MessagingTemplate">
<property name="defaultChannel" ref="outbound-requests"/>
<property name="receiveTimeout" value="100000"/>
</bean>
</property>
</bean>
<int:channel id="outbound-requests"/>
<int-jms:outbound-channel-adapter destination="requestsQueue"
channel="outbound-requests"/>

mbeanExporter Invocation of init method failed UnableToRegisterMBeanException… is javax.management.InstanceAlreadyExistsException

I am getting "mbeanExporter Invocation of init method failed UnableToRegisterMBeanException… InstanceAlreadyExistsException" while trying to start a very simple Spring Batch Admin application in Websphere 8. The entire application can be downloaded from https://examples.javacodegeeks.com/enterprise-java/spring/spring-batch-admin-tutorial/.
This is a very simple Spring Batch Admin project with only one very simple job. I can successfully debug the project in Eclipse and I can build the war, deploy it to Websphere Liberty Profile 8.5 successfuly. By success I mean I can open the Spring Batch Admin console and trigger the job properly.
The error below I am getting only when I move such war to Mainframe Websphere 8.5 ND. I don't think it is direct related to mainframe/unix. In fact, I am wondering if it is not a conflict between the libraries described below in my Pom.xml and Websphere. Any idea what to check will be extremely appreciatted. Recently, I have certain issue when using Hibernate/JPA and by simply downgrade a bit the version fix the error. It was a bit similar scenario: works in Websphere Liberty Profile but didn't work in Websphere 8.5 ND until I downgrade the hibernate version in my pom.xml. Well, this time there is no hibernate at all but I typed here what fixed sometime ago and maybe it can help someone to give me some idea what to check. I have a scratch idea that it is library conflict but I don't know what to check or investigate.
I saw few people getting similar error by using Spring Boot but that isn't my case at all. Specially in Getting exception while refreshing Spring ApplicationContext in Spring Boot application someone got exactly same error as me after added
#EnableIntegrationMBeanExport(registration = RegistrationPolicy.REPLACE_EXISTING)
Nevertheless, I don't have any anotation in such project and I am not using EnableIntegrationMBeanExport at least explicitly. Maybe it is been used behind the scene but I am not that expert on it so if someone can give me possibilities to check it will be an excellent north. Additionally, I am not using Spring MVC.
Error when startuping the application
Trace: 2016/09/09 22:49:17.049 02 t=9BDE88 c=UNK key=P8 tag= (13007004)
SourceId: com.ibm.ws.webcontainer.webapp.WebApp.logServletError
ExtendedMessage: BBOO0220E: SRVE0293E: ÝServlet Error¨-ÝBatch Servlet¨: org.springframework.beans.factory.BeanCreationException: E
rror creating bean with name 'mbeanExporter': Invocation of init method failed; nested exception is org.springframework.jmx.export.U
nableToRegisterMBeanException: Unable to register MBean Ýorg.springframework.integration.monitor.IntegrationMBeanExporter#4ed60bcf¨
with key 'integrationMBeanExporter'; nested exception is javax.management.InstanceAlreadyExistsException: spring.application:cell=dt
l85cel,name=integrationMBeanExporter,type=IntegrationMBeanExporter,node=wlemyAppa,process=WLEmyApp
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.j
ava:1553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.jav
a:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:
475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:70
3)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:7
60)
...
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanSe
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:814)
at com.ibm.ws390.management.connector.corba.CorbaConnectorImpl.invoke(CorbaCon
at com.ibm.ws390.management.connector.corba._CorbaConnectorImplBase._invoke(_C
at com.ibm.ws390.orb.CommonBridge.invoke(CommonBridge.java:1898)
at com.ibm.ws390.orb.CommonBridge.getAndProcessWork(CommonBridge.java:725)
at com.ibm.ws390.orb.CommonBridge.runApplicationThread(CommonBridge.java:614)
at com.ibm.ws390.management.connector.corba.CorbaConnectorImpl.invoke(CorbaCon
at com.ibm.ws390.management.connector.corba._CorbaConnectorImplBase._invoke(_C
at com.ibm.ws390.orb.CommonBridge.invoke(CommonBridge.java:1898)
at com.ibm.ws390.orb.CommonBridge.getAndProcessWork(CommonBridge.java:725)
at com.ibm.ws390.orb.CommonBridge.runApplicationThread(CommonBridge.java:614)
at com.ibm.ws.util.ThreadPool$ZOSWorker.run(ThreadPool.java:2116)
Caused by: org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean Ýorg.springframework.integration.
monitor.IntegrationMBeanExporter#4ed60bcf¨ with key 'integrationMBeanExporter';nested exception is javax.management.InstanceAlready
ExistsException: spring.application:cell=mycel,name=integrationMBeanExporter,type=IntegrationMBeanExporter,node=wlemyAppa,process=
WLEmyApp
pom.xml
<spring.version>4.0.5.RELEASE</spring.version>
<spring.batch.version>3.0.4.RELEASE</spring.batch.version>
<spring.jdbc.version>4.0.5.RELEASE</spring.jdbc.version>
<hsql.version>1.8.0.7</hsql.version>
<commons.version>1.4</commons.version>
<spring.oxm.version>4.0.5.RELEASE</spring.oxm.version>
<spring.batch.admin>1.3.1.RELEASE</spring.batch.admin>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>${spring.batch.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>${hsql.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.jdbc.version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>${commons.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.oxm.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-admin-manager</artifactId>
<version>${spring.batch.admin}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-context-support</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-admin-resources</artifactId>
<version>${spring.batch.admin}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-context-support</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<finalName>SpringBatchAdmin</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Job-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch" xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:batch-default.properties
</value>
</list>
</property>
<property name="searchSystemEnvironment" value="true" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
<job id="myjob" xmlns="http://www.springframework.org/schema/batch"
restartable="true">
<step id="myStep" allow-start-if-complete="true">
<tasklet>
<chunk reader="cvsFileItemReader" writer="itemWriter"
commit-interval="1000" />
</tasklet>
</step>
</job>
<bean id="cvsFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
<!-- property name="resource" value="${INPUT_FILE_LOCATION}"></property-->
<property name="resource" value="file:/C:/temp/sampleData.csv"></property>
<property name="linesToSkip" value="1"></property>
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<!-- <property name="names" value="${INCOMING_COLUMN_MAPPING}"></property> -->
<property name="names" value="firstName,lastName,city,id"></property>
</bean>
</property>
<property name="fieldSetMapper">
<bean
class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<property name="prototypeBeanName" value="userModel"></property>
</bean>
</property>
</bean>
</property>
</bean>
<bean id="itemWriter"
class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<property name="dataSource" ref="dataSource"></property>
<property name="sql" value="${INSERT_QUERY}">
</property>
<property name="itemSqlParameterSourceProvider">
<bean
class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
</property>
</bean>
<bean id="userModel" class="com.javacodegeeks.example.util.UserModel"
scope="prototype" />
</beans>
Contex-config.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<!-- using in-memory store for batch meta-data -->
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseType" value="hsql" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
lazy-init="true" destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url"
value="jdbc:hsqldb:file:src/main/resources/hsqldb/batchcore.db;shutdown=true;" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
</beans>
**** Edited:
I am reading Deploying two Spring batch applications in same cluster in a single Weblogic Domain? which explain how to make two Spring Batch Admin run in Weblogic.
I can see that the person was facing exactly same issue. After that I created another Spring Batch Admin and deployed to my local Websphere Liberty Profile then I got exact same error I was picking up in mainframe.
Firstly it made happy because I thought I finally found the problem and solution.
I fixed in my local websphere by adding /META-INF/spring/batch/override/jmx-context.xml with:
<?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:int-jmx="http://www.springframework.org/schema/integration/jmx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/integration/jmx http://www.springframework.org/schema/integration/jmx/spring-integration-jmx.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<int-jmx:mbean-export id="integrationMBeanExporter" default-domain="SpringBatchAdmin" server="mbeanServer" />
<context:mbean-export default-domain="SpringBatchAdmin" server="mbeanServer" />
</beans>
Well that was great news untill I tried same approach in Websphere 8.5 ND running in mainframe. Exactly same issue remains there. Please, I am desparetly looking for anything else I can at least try.
had the same problem.
I have virtual hosting on the same machine where spring integration was used. Once deployed the test system everything was Ok. After a while, I deployed the prod system and all went wrong.
I specified at Application.class the following:
#EnableIntegrationMBeanExport(defaultDomain = "${managed.domain}")
and in the application.properties file I defined managed.domain the name.
The solution I've found here:
Tutorial

Deploying multiple war files into jetty8

How do I deploy multiple webapps WAR files into Jetty 8 with maven-jetty-plugin?
<contextHandlers>
<contextHandler implementation="org.mortbay.jetty.webapp.WebAppContext">
<war>${basedir}/dir/mywar.war</war>
<contextPath>/path</contextPath>
</contextHandler>
Seems to work only on older plugin versions.
Use the following snippet from a pom.xml. This is adapted from the Jetty server instructions, and although it's for Jetty7 it can easily be adapted for later versions.
pom.xml
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<!-- Jetty 7.3+ requires Maven 3+ -->
<!-- Keep with Jetty 7.6.0 to avoid startup delays from Servlet API 3.0 -->
<version>7.6.0.RC1</version>
<configuration>
<stopKey>STOP</stopKey>
<stopPort>8009</stopPort>
<scanIntervalSeconds>10</scanIntervalSeconds>
<!-- Provide some JNDI resources (optional) -->
<jettyEnvXml>src/test/resources/jetty-jndi-config.xml</jettyEnvXml>
<!-- Register this application as a context -->
<webAppConfig>
<contextPath>/example</contextPath>
</webAppConfig>
<!-- Allow resources on the test classpath to be available -->
<useTestClasspath>true</useTestClasspath>
<!-- Add in any supporting application contexts (use dependencies section) -->
<contextHandlers>
<!-- Supporting WAR (note the use of a property entry for version, and see the dependency later - also Jetty 7 uses org.eclipse...) -->
<contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
<war>
${settings.localRepository}/org/example/supporting-war/${supporting-war.version}/supporting-war-${supporting-war.version}.war
</war>
<contextPath>/supporting-war</contextPath>
</contextHandler>
</contextHandlers>
<connectors>
<!-- Later versions of Jetty don't require the Connector to be specified -->
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>8080</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
<!-- SSL for localhost support -->
<connector implementation="org.mortbay.jetty.security.SslSocketConnector">
<port>8443</port>
<maxIdleTime>60000</maxIdleTime>
<!-- Provide a local key store for serving up SSL certificates -->
<keystore>src/test/resources/jetty-ssl.keystore</keystore>
<!-- Pick any password you like -->
<password>jetty6</password>
<keyPassword>jetty6</keyPassword>
</connector>
</connectors>
</configuration>
<dependencies>
<!-- This ensures that WAR files are downloaded from the repo -->
<!-- Example supporting WAR -->
<dependency>
<groupId>org.example</groupId>
<artifactId>supporting-war</artifactId>
<version>${supporting-war.version}</version>
<scope>compile</scope>
<type>war</type>
</dependency>
</dependencies>
</plugin>
I've left the SSL and JNDI configuration in there just in case anyone needs to see how they are configured. Obviously, they will need the supporting files. The SSL assumes that you've already created a suitable key store containing an SSL certificate for, say, localhost. The JNDI configuration file is as follows:
jetty-jndi-config.xml
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
<New id="ExampleDB" class="org.mortbay.jetty.plus.naming.Resource">
<Arg>java:jdbc/ExampleDB</Arg>
<Arg>
<New class="com.mchange.v2.c3p0.ComboPooledDataSource">
<Set name="driverClass">oracle.jdbc.driver.OracleDriver</Set>
<Set name="jdbcUrl">jdbc:oracle:thin:#//host:port/schema</Set>
<Set name="user">user</Set>
<Set name="password">password</Set>
<!-- Configure a simple connection test with timeout for subsequent queries -->
<Set name="preferredTestQuery">select 1 from dual</Set>
<Set name="checkoutTimeout">5000</Set>
</New>
</Arg>
</New>
</Configure>
This will allow a JNDI resource lookup using, for example, a Spring bean factory like this:
<bean id="exampleDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:jdbc/ExampleDB"/>
<property name="resourceRef" value="true"/>
</bean>
Note that the C3P0 and Oracle references will introduce dependencies that are ostensibly local to your Jetty server, so should be placed in the <plugin><dependencies> section along with the WARs. They don't have to be in the main dependencies.
So now your Maven build will contain an embedded Jetty web server, configured to work with multiple WARs, all tied into the pom.xml version, providing both HTTP and HTTPS and backed with a pooled database connection. That's pretty much everything you need right out of the box for an integrated development environment.

Resources