在ActiveMQ为了方便的切换数据库,更为了深入了解ActiveMQ中SQL语句的详细的信息,可以通过Statements获取各种SQL语句。在ActivMQ第一次加载的时候,通过Statements生产响应的Spring Bean,加载到内存中。在ActiveMQ的管理和监控的时候,从内容获取相关的SQL语句,简化了ActiveMQ的消息的管理和监控。
- package com.easyway.activemq.proxy;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.ArrayList;
- import java.util.regex.Pattern;
- import org.apache.activemq.store.jdbc.Statements;
- /**
- * 获取ActiveMQ中SQL相关的各种语句
- * @author longgangbai
- *
- */
- public class ActiveMQSQLManager {
- /**
- *
- * @param statement
- * @return
- */
- public static String returnStatement(Object statement){
- return ((String)statement).replace("<", "<").replace(">", ">");
- }
- /**
- *
- * @param args
- */
- public static void main(String[] args) throws Exception{
- createSQLForActiveMQ();
- }
- /**
- * 创建ActiveMQ中语句
- * @throws IllegalAccessException
- * @throws InvocationTargetException
- */
- private static void createSQLForActiveMQ() throws IllegalAccessException, InvocationTargetException {
- Statements s=new Statements();
- //设置表的前缀
- s.setTablePrefix("ACTIVEMQ.");
- //获取Statements中创建的语句的集合
- String[] stats=s.getCreateSchemaStatements();
- //构建Statements的Bean
- System.out.println("<bean id=\"statements\" class=\"org.apache.activemq.store.jdbc.Statements\">");
- createSQLForTableIndex(stats);
- createSQLForActiveMQBussine(s);
- //end of generating because of typo
- createSQLOfDropTable(s);
- System.out.println("</bean>");
- }
- /**
- * 创建ActiveMQ监控和管理使用的SQL语句
- * @param s
- * @throws IllegalAccessException
- * @throws InvocationTargetException
- */
- private static void createSQLForActiveMQBussine(Statements s) throws IllegalAccessException, InvocationTargetException {
- //获取Statements的方法
- Method[] methods=Statements.class.getMethods();
- //
- Pattern sPattern= Pattern.compile("get.*Statement$");
- Pattern setPattern= Pattern.compile("set.*Statement$");
- //获取存储Set方法
- ArrayList<String> setMethods=new ArrayList<String>();
- for(int i=0; i<methods.length;i++){
- if(setPattern.matcher(methods[i].getName()).find()){
- setMethods.add(methods[i].getName());
- }
- }
- //
- for(int i=0; i<methods.length;i++){
- if(sPattern.matcher(methods[i].getName()).find()&&setMethods.contains(methods[i].getName().replace("get","set"))){
- System.out.println("<property name=\""+methods[i].getName().substring(3,4).toLowerCase()+methods[i].getName().substring(4)+"\" value=\""+returnStatement(methods[i].invoke(s, (Object[])null))+"\" />");
- }
- }
- //for a typo is not needed if removeMessageStatment typo is corrected
- //获取get的方法
- Pattern sPattern2= Pattern.compile("get.*Statment$");
- for(int i=0; i<methods.length;i++){
- if(sPattern2.matcher(methods[i].getName()).find()){
- System.out.println("<property name=\""+methods[i].getName().substring(3,4).toLowerCase()+methods[i].getName().substring(4)+"\" value=\""+returnStatement(methods[i].invoke(s, (Object[])null))+"\" />");
- }
- }
- }
- /**
- * 创建表的创建语句和索引语句
- * @param stats
- */
- private static void createSQLForTableIndex(String[] stats) {
- //获取创建表和索引的时候的Schema语句
- System.out.println("<property name=\"createSchemaStatements\">");
- System.out.println("<list>");
- for(int i=0; i<stats.length;i++){
- System.out.println("<value>"+stats[i]+"</value>");
- }
- System.out.println("</list>");
- System.out.println("</property>");
- }
- /**
- * 获取删除语句
- * @param s
- */
- private static void createSQLOfDropTable(Statements s) {
- //构建删除语句
- String[] statsDrop=s.getDropSchemaStatements();
- System.out.println("<property name=\"dropSchemaStatements\">");
- System.out.println("<list>");
- for(int i=0; i<statsDrop.length;i++){
- System.out.println("<value>"+statsDrop[i]+"</value>");
- }
- System.out.println("</list>");
- System.out.println("</property>");
- }
- }
生成的spring bean如下:
- <bean id="statements" class="org.apache.activemq.store.jdbc.Statements">
- <property name="createSchemaStatements">
- <list>
- <value>CREATE TABLE ACTIVEMQ.ACTIVEMQ_MSGS(ID BIGINT NOT NULL, CONTAINER VARCHAR(250), MSGID_PROD VARCHAR(250), MSGID_SEQ BIGINT, EXPIRATION BIGINT, MSG BLOB, PRIMARY KEY ( ID ) )</value>
- <value>CREATE INDEX ACTIVEMQ.ACTIVEMQ_MSGS_MIDX ON ACTIVEMQ.ACTIVEMQ_MSGS (MSGID_PROD,MSGID_SEQ)</value>
- <value>CREATE INDEX ACTIVEMQ.ACTIVEMQ_MSGS_CIDX ON ACTIVEMQ.ACTIVEMQ_MSGS (CONTAINER)</value>
- <value>CREATE INDEX ACTIVEMQ.ACTIVEMQ_MSGS_EIDX ON ACTIVEMQ.ACTIVEMQ_MSGS (EXPIRATION)</value>
- <value>CREATE TABLE ACTIVEMQ.ACTIVEMQ_ACKS(CONTAINER VARCHAR(250) NOT NULL, SUB_DEST VARCHAR(250), CLIENT_ID VARCHAR(250) NOT NULL, SUB_NAME VARCHAR(250) NOT NULL, SELECTOR VARCHAR(250), LAST_ACKED_ID BIGINT, PRIMARY KEY ( CONTAINER, CLIENT_ID, SUB_NAME))</value>
- <value>CREATE TABLE ACTIVEMQ.ACTIVEMQ_LOCK( ID BIGINT NOT NULL, TIME BIGINT, BROKER_NAME VARCHAR(250), PRIMARY KEY (ID) )</value>
- <value>INSERT INTO ACTIVEMQ.ACTIVEMQ_LOCK(ID) VALUES (1)</value>
- <value>ALTER TABLE ACTIVEMQ.ACTIVEMQ_MSGS ADD PRIORITY BIGINT</value>
- <value>CREATE INDEX ACTIVEMQ.ACTIVEMQ_MSGS_PIDX ON ACTIVEMQ.ACTIVEMQ_MSGS (PRIORITY)</value>
- <value>ALTER TABLE ACTIVEMQ.ACTIVEMQ_ACKS ADD PRIORITY BIGINT DEFAULT 5 NOT NULL</value>
- <value>ALTER TABLE ACTIVEMQ.ACTIVEMQ_ACKS DROP PRIMARY KEY</value>
- <value>ALTER TABLE ACTIVEMQ.ACTIVEMQ_ACKS ADD PRIMARY KEY (CONTAINER, CLIENT_ID, SUB_NAME, PRIORITY)</value>
- </list>
- </property>
- <property name="addMessageStatement" value="INSERT INTO ACTIVEMQ.ACTIVEMQ_MSGS(ID, MSGID_PROD, MSGID_SEQ, CONTAINER, EXPIRATION, PRIORITY, MSG) VALUES (?, ?, ?, ?, ?, ?, ?)" />
- <property name="updateMessageStatement" value="UPDATE ACTIVEMQ.ACTIVEMQ_MSGS SET MSG=? WHERE ID=?" />
- <property name="findMessageSequenceIdStatement" value="SELECT ID, PRIORITY FROM ACTIVEMQ.ACTIVEMQ_MSGS WHERE MSGID_PROD=? AND MSGID_SEQ=? AND CONTAINER=?" />
- <property name="findMessageStatement" value="SELECT MSG FROM ACTIVEMQ.ACTIVEMQ_MSGS WHERE MSGID_PROD=? AND MSGID_SEQ=?" />
- <property name="findMessageByIdStatement" value="SELECT MSG FROM ACTIVEMQ.ACTIVEMQ_MSGS WHERE ID=?" />
- <property name="findAllMessagesStatement" value="SELECT ID, MSG FROM ACTIVEMQ.ACTIVEMQ_MSGS WHERE CONTAINER=? ORDER BY ID" />
- <property name="findLastSequenceIdInMsgsStatement" value="SELECT MAX(ID) FROM ACTIVEMQ.ACTIVEMQ_MSGS" />
- <property name="lastProducerSequenceIdStatement" value="SELECT MAX(MSGID_SEQ) FROM ACTIVEMQ.ACTIVEMQ_MSGS WHERE MSGID_PROD=?" />
- <property name="findLastSequenceIdInAcksStatement" value="SELECT MAX(LAST_ACKED_ID) FROM ACTIVEMQ.ACTIVEMQ_ACKS" />
- <property name="createDurableSubStatement" value="INSERT INTO ACTIVEMQ.ACTIVEMQ_ACKS(CONTAINER, CLIENT_ID, SUB_NAME, SELECTOR, LAST_ACKED_ID, SUB_DEST, PRIORITY) VALUES (?, ?, ?, ?, ?, ?, ?)" />
- <property name="findDurableSubStatement" value="SELECT SELECTOR, SUB_DEST FROM ACTIVEMQ.ACTIVEMQ_ACKS WHERE CONTAINER=? AND CLIENT_ID=? AND SUB_NAME=?" />
- <property name="findAllDurableSubsStatement" value="SELECT SELECTOR, SUB_NAME, CLIENT_ID, SUB_DEST FROM ACTIVEMQ.ACTIVEMQ_ACKS WHERE CONTAINER=? AND PRIORITY=0" />
- <property name="updateLastPriorityAckRowOfDurableSubStatement" value="UPDATE ACTIVEMQ.ACTIVEMQ_ACKS SET LAST_ACKED_ID=? WHERE CONTAINER=? AND CLIENT_ID=? AND SUB_NAME=? AND PRIORITY=?" />
- <property name="deleteSubscriptionStatement" value="DELETE FROM ACTIVEMQ.ACTIVEMQ_ACKS WHERE CONTAINER=? AND CLIENT_ID=? AND SUB_NAME=?" />
- <property name="findAllDurableSubMessagesStatement" value="SELECT M.ID, M.MSG FROM ACTIVEMQ.ACTIVEMQ_MSGS M, ACTIVEMQ.ACTIVEMQ_ACKS D WHERE D.CONTAINER=? AND D.CLIENT_ID=? AND D.SUB_NAME=? AND M.CONTAINER=D.CONTAINER AND M.ID > D.LAST_ACKED_ID ORDER BY M.PRIORITY DESC, M.ID" />
- <property name="findDurableSubMessagesStatement" value="SELECT M.ID, M.MSG FROM ACTIVEMQ.ACTIVEMQ_MSGS M, ACTIVEMQ.ACTIVEMQ_ACKS D WHERE D.CONTAINER=? AND D.CLIENT_ID=? AND D.SUB_NAME=? AND M.CONTAINER=D.CONTAINER AND M.ID > D.LAST_ACKED_ID AND M.ID > ? ORDER BY M.ID" />
- <property name="nextDurableSubscriberMessageStatement" value="SELECT M.ID, M.MSG FROM ACTIVEMQ.ACTIVEMQ_MSGS M, ACTIVEMQ.ACTIVEMQ_ACKS D WHERE D.CONTAINER=? AND D.CLIENT_ID=? AND D.SUB_NAME=? AND M.CONTAINER=D.CONTAINER AND M.ID > ? ORDER BY M.ID " />
- <property name="durableSubscriberMessageCountStatement" value="SELECT COUNT(*) FROM ACTIVEMQ.ACTIVEMQ_MSGS M, ACTIVEMQ.ACTIVEMQ_ACKS D WHERE D.CONTAINER=? AND D.CLIENT_ID=? AND D.SUB_NAME=? AND M.CONTAINER=D.CONTAINER AND M.ID > ( SELECT LAST_ACKED_ID FROM ACTIVEMQ.ACTIVEMQ_ACKS WHERE CONTAINER=D.CONTAINER AND CLIENT_ID=D.CLIENT_ID AND SUB_NAME=D.SUB_NAME )" />
- <property name="findAllDestinationsStatement" value="SELECT DISTINCT CONTAINER FROM ACTIVEMQ.ACTIVEMQ_ACKS" />
- <property name="removeAllMessagesStatement" value="DELETE FROM ACTIVEMQ.ACTIVEMQ_MSGS WHERE CONTAINER=?" />
- <property name="removeAllSubscriptionsStatement" value="DELETE FROM ACTIVEMQ.ACTIVEMQ_ACKS WHERE CONTAINER=?" />
- <property name="deleteOldMessagesStatement" value="DELETE FROM ACTIVEMQ.ACTIVEMQ_MSGS WHERE ( EXPIRATION<>0 AND EXPIRATION<?) OR (ID <= ( SELECT min(ACTIVEMQ.ACTIVEMQ_ACKS.LAST_ACKED_ID) FROM ACTIVEMQ.ACTIVEMQ_ACKS WHERE ACTIVEMQ.ACTIVEMQ_ACKS.CONTAINER=ACTIVEMQ.ACTIVEMQ_MSGS.CONTAINER ) )" />
- <property name="lockCreateStatement" value="SELECT * FROM ACTIVEMQ.ACTIVEMQ_LOCK FOR UPDATE" />
- <property name="lockUpdateStatement" value="UPDATE ACTIVEMQ.ACTIVEMQ_LOCK SET TIME = ? WHERE ID = 1" />
- <property name="destinationMessageCountStatement" value="SELECT COUNT(*) FROM ACTIVEMQ.ACTIVEMQ_MSGS WHERE CONTAINER=?" />
- <property name="findNextMessagesStatement" value="SELECT ID, MSG FROM ACTIVEMQ.ACTIVEMQ_MSGS WHERE CONTAINER=? AND ID > ? ORDER BY ID" />
- <property name="lastAckedDurableSubscriberMessageStatement" value="SELECT MAX(LAST_ACKED_ID) FROM ACTIVEMQ.ACTIVEMQ_ACKS WHERE CONTAINER=? AND CLIENT_ID=? AND SUB_NAME=?" />
- <property name="selectDurablePriorityAckStatement" value="SELECT LAST_ACKED_ID FROM ACTIVEMQ.ACTIVEMQ_ACKS WHERE CONTAINER=? AND CLIENT_ID=? AND SUB_NAME=? AND PRIORITY = ?" />
- <property name="insertDurablePriorityAckStatement" value="INSERT INTO ACTIVEMQ.ACTIVEMQ_ACKS(CONTAINER, CLIENT_ID, SUB_NAME, PRIORITY) VALUES (?, ?, ?, ?)" />
- <property name="updateDurableLastAckStatement" value="UPDATE ACTIVEMQ.ACTIVEMQ_ACKS SET LAST_ACKED_ID = ? WHERE CONTAINER=? AND CLIENT_ID=? AND SUB_NAME=?" />
- <property name="dropSchemaStatements">
- <list>
- <value>DROP TABLE ACTIVEMQ.ACTIVEMQ_ACKS</value>
- <value>DROP TABLE ACTIVEMQ.ACTIVEMQ_MSGS</value>
- <value>DROP TABLE ACTIVEMQ.ACTIVEMQ_LOCK</value>
- </list>
- </property>
- </bean>