任何人都知道如何在Mule中实现持久对象存储到数据库?即一个对象存储库使用mule的接口,但是由一些数据库支持,例如,甲骨文
答案 0 :(得分:1)
以下是使用由数据库支持的对象库的完整配置,在本例中为MySQL。
该表如下所示
CREATE TABLE `objectstore` (
`keyy` varchar(255) NOT NULL,
`value` varchar(255) DEFAULT NULL,
PRIMARY KEY (`keyy`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
<spring:beans>
<spring:bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
<spring:property name="driverClass" value="com.mysql.jdbc.Driver"></spring:property>
<spring:property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></spring:property>
<spring:property name="username" value="root"></spring:property>
<spring:property name="password" value="root"></spring:property>
<spring:property name="maxConnectionsPerPartition" value="50"></spring:property>
<spring:property name="minConnectionsPerPartition" value="10"></spring:property>
<spring:property name="acquireIncrement" value="10"></spring:property>
<spring:property name="partitionCount" value="3"></spring:property>
<spring:property name="statementsCacheSize" value="20"></spring:property>
</spring:bean>
<spring:bean name="jdbcStore" class="org.mule.transport.jdbc.store.JdbcObjectStore">
<spring:property name="jdbcConnector" ref="jdbcConnector"></spring:property>
<spring:property name="insertQueryKey" value="insertQueryKey"></spring:property>
<spring:property name="selectQueryKey" value="selectQueryKey"></spring:property>
<spring:property name="deleteQueryKey" value="deleteQueryKey"></spring:property>
<spring:property name="clearQueryKey" value="clearQueryKey"></spring:property>
</spring:bean>
</spring:beans>
<jdbc-ee:connector name="jdbcConnector" pollingFrequency="1000" dataSource-ref="dataSource" transactionPerMessage="false" doc:name="Database">
<jdbc-ee:query key="insertQueryKey" value="insert into objectstore (keyy,value) values (?,?)"></jdbc-ee:query>
<jdbc-ee:query key="selectQueryKey" value="select key,value from objectstore where keyy = ?"></jdbc-ee:query>
<jdbc-ee:query key="deleteQueryKey" value="delete from objectstore where keyy = ?"></jdbc-ee:query>
<jdbc-ee:query key="clearQueryKey" value="delete from objectstore"></jdbc-ee:query>
</jdbc-ee:connector>
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"></http:listener-config>
<objectstore:config name="ObjectStore" doc:name="ObjectStore" objectStore-ref="jdbcStore"></objectstore:config>
<flow name="persistent-tokenFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="insert" doc:name="HTTP"></http:listener>
<set-variable variableName="key" value="new key" doc:name="key"></set-variable>
<set-variable variableName="value" value="new value" doc:name="value"></set-variable>
<objectstore:store config-ref="ObjectStore" key="#[flowVars.key]" value-ref="#[flowVars.value]" doc:name="ObjectStore"></objectstore:store>
</flow>
答案 1 :(得分:1)
Sudarshan的榜样很棒。请注意,org.mule.transport.jdbc.store.JdbcObjectStore
未实现org.mule.api.store.ListableObjectStore
,如果您使用allKeys
操作,则会抛出异常。
以下文章显示了实现ListableObjectStore的示例:
http://www.ricston.com/blog/mule-object-store-jdbc/
帖子中提到的回购:
https://github.com/Ricston/mule-object-store-jdbc
TL; DR ,在您自己的类中复制粘贴JdbcObjectStore,实现ListableObjectStore(不能扩展其私有字段),并添加以下内容:
protected ColumnListHandler listHandler = new ColumnListHandler();
protected String allKeysQueryKey;
public String getAllKeysQueryKey() {
return allKeysQueryKey;
}
public void setAllKeysQueryKey(String allKeysQueryKey) {
this.allKeysQueryKey = allKeysQueryKey;
}
@SuppressWarnings("unchecked")
@Override
public List<Serializable> allKeys() throws ObjectStoreException {
String allKeysQuery = this.getAllKeysQuery();
List<String> result = (List<String>) this.query(allKeysQuery, this.listHandler);
return new ArrayList<Serializable>(result);
}
@Override
public void open() throws ObjectStoreException {
// no need to implement: JdbcConnector should take care of opening connection to data source
}
@Override
public void close() throws ObjectStoreException {
// no need to implement: JdbcConnector should take care of closing connection to data source
}
继续Sudarshan的例子,然后你将以下设置添加到你的jdbcStore:
<spring:property name="allKeysQueryKey" value="allKeysQueryKey" />
以及对jdbcConnector的以下查询:
<jdbc-ee:query key="allKeysQueryKey" value="select keyy from objectstore"></jdbc-ee:query>
P.S将Transformer添加到此类也很方便,您可以为其分配Mule的字节数组到对象转换器。这样,您可以避免在每次allKeys
操作后向流中显式添加一个。上面的帖子/回购就是一个例子。
答案 2 :(得分:0)
您可以通过扩展AbstractObjectStore或其中一个子类来实现一个。我自己从未使用过JdbcObjectStore,但至少有一个示例配置可供您了解如何使用JdbcConnector和所需查询进行配置:http://mulesoft.github.io/sap-transport/