- 策略模式进行设计如下
public class StrategyContext {
private static SendStrategy mqStrategy = null;
private static SendStrategy postStrategy = null;
static {
mqStrategy = new MqStrategy();
postStrategy = new PostStrategy();
}
public static boolean sendData(String data, String url, String appType,
String sendWay) {
if ("MqStrategy".equals(sendWay)) {
Log.debug("MqStrategy 方式发送数据");
return mqStrategy.send(data, url, appType);
} else if ("PostStrategy".equals(sendWay)) {
Log.debug("PostStrategy 方式发送数据");
return postStrategy.send(data, url, appType);
} else {
Log.info("没有指定发送方式!!!sendWay:" + sendWay);
return false;
}
}
}
- Post采用Http协议,转发的代码实现:
package com.cloud.strategy;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import com.cloud.util.Log;
public class PostStrategy implements SendStrategy {
@Override
public boolean send(String data, String url, String appType) {
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(url);
boolean isSuccess = false;
try {
System.out.println(url);
post.getParams().setParameter(
HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
post.addParameter("data", data);
Log.debug(this.getClass().getSimpleName() + " begin send data...");
Log.debug(this.getClass().getSimpleName() + " send data : " + data);
// 设置连接超时时间
client.getHttpConnectionManager().getParams()
.setConnectionTimeout(10000);
// 设置响应超时时间
client.getHttpConnectionManager().getParams().setSoTimeout(15000);
int returnFlag = client.executeMethod(post);
if (returnFlag == 200) {
isSuccess = true;
}
Log.info(this.getClass().getSimpleName()
+ " success receive form post: "
+ post.getStatusLine().toString() + ",returnFlag="
+ returnFlag);
} catch (Exception e) {
isSuccess = false;
Log.error(this.getClass().getSimpleName()
+ " fail receive form post: " + e.getMessage());
} finally {
if (post != null) {
post.releaseConnection();
Log.info(this.getClass().getSimpleName()
+ " post.releaseConnection() " + "is coming");
}
}
return isSuccess;
}
}
- MQ的代码实现如下:
public class MqStrategy implements SendStrategy {
static ObsDataMsgPublisher publisher = null;
static {
publisher = ObsDataMsgPublisher.getInstance();
}
@Override
public boolean send(String data, String url, String appType) {
if (publisher == null) {
return false;
}
String queueName = PropertiesReader.getProp("jms.queue." + appType);
System.out.println("queueName="+queueName);
if (queueName == null || "".equals(queueName)) {
return false;
}
return publisher.sendByQuene(data, queueName);
}
}
- sendByQuene的代码实现如下:
package com.cloud.strategy;
import java.net.URLEncoder;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.jms.JmsException;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import com.cloud.util.PropertiesReader;
public class ObsDataMsgPublisher implements MessagePublisher {
private static ObsDataMsgPublisher _instance = null;
private static JmsTemplate jmsTemplate;
private ObsDataMsgPublisher() {
};
public static synchronized ObsDataMsgPublisher getInstance() {
if (_instance == null) {
_instance = new ObsDataMsgPublisher();
ActiveMQConnectionFactory mqFactory = new ActiveMQConnectionFactory();
mqFactory.setBrokerURL(PropertiesReader.getProp("jms.url"));
CachingConnectionFactory cachFactory = new CachingConnectionFactory(
mqFactory);
cachFactory.setSessionCacheSize(Integer.parseInt(PropertiesReader
.getProp("jms.cachSessionNum")));
jmsTemplate = new JmsTemplate(cachFactory);
jmsTemplate.setPubSubDomain(false);// p2p方式
jmsTemplate.setDeliveryMode(DeliveryMode.PERSISTENT);// 采用持久化方式
}
return _instance;
}
@Override
public synchronized boolean sendByQuene(final String msg,
final String queueName) {
boolean ret = false;
try {
log.info(this.getClass().getSimpleName() + "--准备发送JMS消息,queueName:"
+ queueName);
log.info(this.getClass().getSimpleName() + "--msg :" + msg);
jmsTemplate.setDefaultDestinationName(queueName);
jmsTemplate.send(new MessageCreator() {
@Override
public Message createMessage(Session session)
throws JMSException {
try {
return session.createTextMessage(URLEncoder.encode(msg,
"utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
});
ret = true;
log.info(this.getClass().getSimpleName() + "--JMS消息发送成功");
} catch (JmsException e) {
e.printStackTrace();
}
return ret;
}
}