提交 763a4603 编写于 作者: M MaxKey

Provision topic to database

上级 de6c72ff
......@@ -19,11 +19,14 @@ package org.maxkey.provision;
public class ProvisionMessage {
String id;
String topic;
String actionType;
String sendTime;
Object content;
String id;
String topic;
String actionType;
String sendTime;
String content;
int connected;
Object sourceObject;
public String getTopic() {
return topic;
......@@ -61,20 +64,37 @@ public class ProvisionMessage {
return content;
}
public void setContent(Object content) {
public void setContent(String content) {
this.content = content;
}
public ProvisionMessage() {
public int getConnected() {
return connected;
}
public void setConnected(int connected) {
this.connected = connected;
}
public Object getSourceObject() {
return sourceObject;
}
public void setSourceObject(Object sourceObject) {
this.sourceObject = sourceObject;
}
public ProvisionMessage() {
}
public ProvisionMessage(String id,String topic, String actionType, String sendTime, Object content) {
public ProvisionMessage(String id,String topic, String actionType, String sendTime, String content,Object sourceObject) {
super();
this.id = id;
this.topic = topic;
this.actionType = actionType;
this.sendTime = sendTime;
this.content = content;
this.sourceObject = sourceObject;
}
......
......@@ -22,10 +22,10 @@ import java.util.UUID;
import org.maxkey.configuration.ApplicationConfig;
import org.maxkey.provision.thread.ProvisioningThread;
import org.maxkey.util.DateUtils;
import org.maxkey.util.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
......@@ -33,17 +33,10 @@ public class ProvisionService {
private static final Logger _logger = LoggerFactory.getLogger(ProvisionService.class);
@Autowired
protected ApplicationConfig applicationConfig;
ApplicationConfig applicationConfig;
public void setApplicationConfig(ApplicationConfig applicationConfig) {
this.applicationConfig = applicationConfig;
}
public ApplicationConfig getApplicationConfig() {
return applicationConfig;
}
@Autowired
JdbcTemplate jdbcTemplate;
/**
* send msg to jdbc
......@@ -60,18 +53,31 @@ public class ProvisionService {
topic, //TOPIC
actionType, //action of content
DateUtils.getCurrentDateTimeAsString(), //send time
content //content Object to json message content
null, //content Object to json message content
content
);
String msg = JsonUtils.gson2Json(message);
//sand msg to provision topic
Thread thread = null;
if(applicationConfig.getMessageQueue().equalsIgnoreCase("provision")) {
_logger.trace("message...");
thread = new ProvisioningThread(topic,msg);
thread = new ProvisioningThread(jdbcTemplate,message);
thread.start();
}else{
_logger.trace("no send message...");
}
thread.start();
}
}
public void setApplicationConfig(ApplicationConfig applicationConfig) {
this.applicationConfig = applicationConfig;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public ApplicationConfig getApplicationConfig() {
return applicationConfig;
}
}
......@@ -16,9 +16,15 @@
package org.maxkey.provision.thread;
import org.maxkey.pretty.PrettyFactory;
import java.io.Serializable;
import java.sql.Types;
import org.maxkey.pretty.impl.JsonPretty;
import org.maxkey.provision.ProvisionMessage;
import org.maxkey.util.ObjectTransformer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* Provisioning Thread for send message
......@@ -27,21 +33,31 @@ import org.slf4j.LoggerFactory;
public class ProvisioningThread extends Thread{
private static final Logger _logger = LoggerFactory.getLogger(ProvisioningThread.class);
String topic ;
static final String PROVISION_INSERT_STATEMENT = "insert into mxk_history_provisions(`id`,`topic`,`actiontype`,`content`,`sendtime`,`connected`) values (? , ? , ? , ? , ? , ? )";
JdbcTemplate jdbcTemplate;
String msg;
ProvisionMessage msg;
public ProvisioningThread(
String topic,
String msg) {
this.topic = topic;
public ProvisioningThread(JdbcTemplate jdbcTemplate,
ProvisionMessage msg) {
this.jdbcTemplate = jdbcTemplate;
this.msg = msg;
}
@Override
public void run() {
_logger.debug("send message \n{}" , PrettyFactory.getJsonPretty().format(msg));
//kafkaTemplate.send(topic, msg);
_logger.debug("send message \n{}" ,new JsonPretty().jacksonFormat(msg.getSourceObject()));
msg.setContent(ObjectTransformer.serialize((Serializable)msg.getSourceObject()));
jdbcTemplate.update(PROVISION_INSERT_STATEMENT,
new Object[] {
msg.getId(), msg.getTopic(), msg.getActionType(), msg.getContent(),
msg.getSendTime(),msg.getConnected()
},
new int[] {
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.TINYINT
});
_logger.debug("send to Message Queue finished .");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册