Archive for activemq

ActiveMQ 5 com persistência JDBC em Oracle

Configurando o apache activemq 5.5.1 com persistência JDBC no Oracle 11g

Você precisará configurar, corretamente, apenas 2 arquivos:

  • activemq.xml
  • activemq-jdbc.xml
  • camel.xml

O camel.xml será utilizado apenas para o jmsloader, usado no stress test do activemq que iremos fazer ;)

Primeiro, baixe o driver oracle jdbc, ojdbc6.jar e armazene ele dentro de activemq/lib/optional

Arquivo activemq.xml

Adicionado a linha: 25, importando o arquivo camel.xml (ele será usado para jmsloader)
Comentei o bloco iniciado na linha: 50, destinationPolicy. Note que a linha 60 também leva um comentário, pois no xml um comentário dentro de comentário vai dar zica.
Configurei a linha: 93, jdbcPersistenceAdapter, vital para o conector persistente jdbc.
Adicionado as linhas: 129 130 e 131, amq:connectionFactory, para testes com o jmsloader)
Adicionado a linha: 139, o

<!--
    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: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-2.0.xsd
  http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">

<import resource="camel.xml"/>

    <!-- 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>

    <!--
        The <broker> element is used to configure the ActiveMQ broker.
    -->
    <broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.base}/data" destroyApplicationContextOnStop="true">

        <!--
                        For better performances use VM cursor and small memory limit.
                        For more information, see:

http://activemq.apache.org/message-cursors.html

            Also, if your producer is "hanging", it's probably due to producer flow control.
            For more information, see:

http://activemq.apache.org/producer-flow-control.html

        -->

        <!--
        <destinationPolicy>
            <policyMap>
              <policyEntries>
                <policyEntry topic=">" producerFlowControl="true" memoryLimit="1mb">
                  <pendingSubscriberPolicy>
                    <vmCursor />
                  </pendingSubscriberPolicy>
                </policyEntry>
                <policyEntry queue=">" producerFlowControl="true" memoryLimit="1mb">
                  ##<!- Use VM cursor for better latency
                       For more information, see:

http://activemq.apache.org/message-cursors.html

                  <pendingQueuePolicy>
                    <vmQueueCursor/>
                  </pendingQueuePolicy>
                  ##->
                </policyEntry>
              </policyEntries>
            </policyMap>
        </destinationPolicy>
         -->

        <!--
            The managementContext is used to configure how ActiveMQ is exposed in
            JMX. By default, ActiveMQ uses the MBean server that is started by
            the JVM. For more information, see: 

http://activemq.apache.org/jmx.html

        -->
        <managementContext>
            <managementContext createConnector="false"/>
        </managementContext>

        <!--
            Configure message persistence for the broker. The default persistence
            mechanism is the KahaDB store (identified by the kahaDB tag).
            For more information, see: 

http://activemq.apache.org/persistence.html

        -->
        <persistenceAdapter>
              <jdbcPersistenceAdapter dataSource="#oracle-ds"/>
        </persistenceAdapter>
          <!--
            The systemUsage controls the maximum amount of space the broker will
            use before slowing down producers. For more information, see:

http://activemq.apache.org/producer-flow-control.html

        <systemUsage>
            <systemUsage>
                <memoryUsage>
                    <memoryUsage limit="20 mb"/>
                </memoryUsage>
                <storeUsage>
                    <storeUsage limit="1 gb"/>
                </storeUsage>
                <tempUsage>
                    <tempUsage limit="100 mb"/>
                </tempUsage>
            </systemUsage>
        </systemUsage>
                -->

        <!--
            The transport connectors expose ActiveMQ over a given protocol to
            clients and other brokers. For more information, see: 

http://activemq.apache.org/configuring-transports.html

        -->
        <transportConnectors>
            <transportConnector name="openwire" uri="tcp://0.0.0.0:61616"/>
        </transportConnectors>

    </broker>

        <amq:connectionFactory brokerURL="tcp://0.0.0.0:61616" id="ConnectionFactory" />
        <amq:queue id="exampleA" name="example.A" physicalName="example.A" />
        <amq:queue id="exampleB" name="example.B" physicalName="example.B" />

    <!--
        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"/>
    <import resource="activemq-jdbc.xml"/>

</beans>
<!--END SNIPPET: example -->

Arquivo activemq-jdbc.xml

alterei as linha: 45, adicionando o novo dataSource oracle-ds
comentei a linha: 49, transportConnectors, pois ele já está definido no arquivo activemq.xml
comentei a linha: 54, o bean derby-ds, pois vamos utilizar o Oracle
configurei o bloco da linha: 88, adicionado configurações do Oracle DataSource

<!--
    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.
-->
<!--
    Use JDBC for message persistence
    For more information, see:

http://activemq.apache.org/persistence.html

    You need to add Derby database to your classpath in order to make this example work.
    Download it from http://db.apache.org/derby/ and put it in the ${ACTIVEMQ_HOME}/lib/optional/ folder
    Optionally you can configure any other RDBM as shown below

    To run ActiveMQ with this configuration add xbean:conf/activemq-jdbc.xml to your command

    e.g. $ bin/activemq console xbean:conf/activemq-jdbc.xml
 -->
<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-2.0.xsd
  http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.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>

  <broker useJmx="false" brokerName="jdbcBroker" xmlns="http://activemq.apache.org/schema/core">
    <persistenceAdapter>
       <jdbcPersistenceAdapter dataDirectory="${activemq.base}/data" dataSource="#oracle-ds"/>
    </persistenceAdapter>

    <!-- transportConnectors>
       <transportConnector name="default" uri="tcp://0.0.0.0:61616"/>
    </transportConnectors -->
  </broker>

  <!-- Embedded Derby DataSource Sample Setup
  <bean id="derby-ds" class="org.apache.derby.jdbc.EmbeddedDataSource">
    <property name="databaseName" value="derbydb"/>
    <property name="createDatabase" value="create"/>
  </bean> -->

  <!-- Postgres DataSource Sample Setup -->
  <!--
  <bean id="postgres-ds" class="org.postgresql.ds.PGPoolingDataSource">
    <property name="serverName" value="localhost"/>
    <property name="databaseName" value="activemq"/>
    <property name="portNumber" value="0"/>
    <property name="user" value="activemq"/>
    <property name="password" value="activemq"/>
    <property name="dataSourceName" value="postgres"/>
    <property name="initialConnections" value="1"/>
    <property name="maxConnections" value="10"/>
  </bean>
  -->

  <!-- MySql DataSource Sample Setup -->
  <!--
  <bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost/activemq?relaxAutoCommit=true"/>
    <property name="username" value="activemq"/>
    <property name="password" value="activemq"/>
    <property name="maxActive" value="200"/>
    <property name="poolPreparedStatements" value="true"/>
  </bean>
  -->

  <!-- Oracle DataSource Sample Setup -->

  <bean id="oracle-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value="jdbc:oracle:thin:@HOST:1521:DBNAME"/>
    <property name="username" value="USERNAME"/>
    <property name="password" value="PASSWD"/>
    <property name="maxActive" value="200"/>
    <property name="poolPreparedStatements" value="true"/>
  </bean>

</beans>

Arquivo camel.xml
Configurado as rotas de fila para o Camel.

 <!--
    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.
-->
<!--

    Lets deploy some Enterprise Integration Patterns inside the ActiveMQ Message Broker
    For more information, see:

http://camel.apache.org

    Include this file in your configuration to enable Camel

    e.g. <import resource="camel.xml"/>

-->
<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">

        <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">

        <!-- You can use a <packages> element for each root package to search for Java routes -->
        <packageScan>
           <package>org.foo.bar</package>
        </packageScan>

        <!-- You can use Spring XML syntax to define the routes here using the <route> element -->
        <route>
            <description>Example Camel Route</description>
            <from uri="activemq:example.A"/>
            <!-- to uri="activemq:example.B"/ -->
            <inOnly uri="activemq:example.B"/>
        </route>
    </camelContext>

    <!--
       Lets configure some Camel endpoints

http://camel.apache.org/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&amp;waitForStart=10000" / -->
            <property name="brokerURL" value="tcp://localhost:61616" />
            <property name="userName" value="${activemq.username}"/>
            <property name="password" value="${activemq.password}"/>
          </bean>
        </property>
    </bean>
</beans>

Mais sobre a Arquitetura ActiveMQ: http://activemq.apache.org/jdbc-master-slave.html