ActiveMQ5.14.1使用PostgreSQL9.6

这是官网的教程:

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

http://activemq.apache.org/jdbc-support.html

我并不是完全按照官网来的,

1,我备份了一份activemq.xml后就在上面改了,否则按官网的说法要每次启动ActiveMQ时都要指定配置文件,觉得这可能会是个坑

2,官网上用的是dbcp2+mysql,我用的是c3p0+postgresql,配置如下



<!--PostgreSQL DataSource By Qbit-->
<bean id="postgreSql-ds" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 
		<property name="driverClass" value="org.postgresql.Driver"/>
<!-- jdbcUrl not jdbcurl  -->
		<property name="jdbcUrl" value="jdbc:postgresql://192.168.60.128:5432/activemq"/>
  		<property name="user" value="activemq"/>
  		<property name="password" value="mima"/>
<!-- the properties are the same with the default value -->
  		<property name="maxPoolSize" value="15"/>
		<property name="minPoolSize" value="3"/>
		<property name="initialPoolSize" value="3"/>
		<property name="maxIdleTime" value="0"/>
<!-- invalid
		<property name="maxIdleTime">0</property>
-->
		<property name="checkoutTimeout" value="0"/>
		<property name="acquireIncrement" value="3"/>
		<property name="acquireRetryAttempts" value="0"/>
	</bean>



后面几个完全是无聊配置的,参考的http://blog.csdn.net/caihaijiang/article/details/6843496

这里也有官方文档http://www.mchange.com/projects/c3p0/index.html


按照http://www.cnblogs.com/chenying99/articles/3172128.html上面的说法,可以使用高效日志

postgres=# create database activemq with owner = activemq encoding='UTF8' tablespace=pg_default lc_collate='zh_CN.UTF-8' connection limit=-1 template template0;
CREATE DATABASE
postgres=# grant connect on database activemq to activemq;
GRANT

activemq=# create schema activemq;
CREATE SCHEMA
activemq=# grant all privileges on schema activemq to activemq;
GRANT
activemq=# alter database activemq set search_path to activemq;
ALTER DATABASE
activemq=# alter role activemq set search_path=activemq;
ALTER ROLE
grant usage on schema activemq to activemq;
grant create on schema activemq to activemq;

貌似上面最后两句还是需要的,那个all privileges不好使.


activemq启动的时候会自动在postgresql里创建相应的三个数据库,到activemq的web控制台发条queue消息,勾上persistence,然后就会在数据库的msgs里看到一条记录,但是消息本身是什么内容是无法看到的,然后启动java程序消费掉那条消息后数据库的数据就没了.

最后附上java代码

import javax.jms.*;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.DeliveryMode;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by qbit on 12/8/16.
 */
public class PersistencedActiveMq implements Callable<Object>,Runnable,MessageListener{
    public static final String user = ActiveMQConnection.DEFAULT_USER;

    public static final String password = ActiveMQConnection.DEFAULT_PASSWORD;
    public static final String SERVER_IP="192.168.60.128";
    public static final String url ="failover://tcp://"+SERVER_IP+":61616";// ActiveMQConnection.DEFAULT_BROKER_URL;

    public static final String subject = "Qbit_message";
    private static final AtomicInteger counter=new AtomicInteger(0);
    public static void main(String[] args) throws InterruptedException {
        ExecutorService pool= Executors.newCachedThreadPool();
        PersistencedActiveMq pam=new PersistencedActiveMq();
        pool.submit((Callable)pam);
        pool.shutdown();
    }

    @Override
    public void run() {
        try {
            final int index=counter.getAndIncrement();
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                    user, password, url);
            Connection connection = connectionFactory.createConnection();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Queue destination = session.createQueue(subject);
            MessageProducer producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
            connection.start();
            for(int i=0;i<5;i++) {
                final String message=subject+':'+index+':'+i;
                TextMessage msg = session.createTextMessage(message);
                System.out.println("Producer:->Sending message: " + message);
                producer.send(msg);
                System.out.println("Producer:->Message sent complete!");
            }
            if (producer != null)
                producer.close();
            if (session != null)
                session.close();
            if (connection != null)
                connection.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public Object call() throws Exception {

        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                user, password, url);
        Connection connection = connectionFactory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue destination = session.createQueue(subject);
        MessageConsumer consumer = session.createConsumer(destination);
        connection.start();
        System.out.println("Consumer:->Begin listening...");
        consumer.setMessageListener(this);
        Thread.sleep(9*1000);
        if (consumer != null)
            consumer.close();
        if (session != null)
            session.close();
        if (connection != null)
            connection.close();
        return null;
    }

    @Override
    public void onMessage(Message message) {
        System.out.println("Consumer:->Received: " + message);
    }
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值