Commit 75256457 by WeiCong

优化jms服务

parent 3fc85bd2
...@@ -9,7 +9,6 @@ import com.brilliance.eibs.util.StringUtil; ...@@ -9,7 +9,6 @@ import com.brilliance.eibs.util.StringUtil;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.math.NumberUtils; import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl; import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.PropertyValue; import org.springframework.beans.PropertyValue;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory; import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
...@@ -23,6 +22,7 @@ import org.springframework.jms.support.converter.SimpleMessageConverter; ...@@ -23,6 +22,7 @@ import org.springframework.jms.support.converter.SimpleMessageConverter;
import javax.jms.ConnectionFactory; import javax.jms.ConnectionFactory;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects;
public class JmsListeningServer extends AbsServer { public class JmsListeningServer extends AbsServer {
static final String DESTINATION_TYPE_QUEUE = "queue"; static final String DESTINATION_TYPE_QUEUE = "queue";
...@@ -40,7 +40,7 @@ public class JmsListeningServer extends AbsServer { ...@@ -40,7 +40,7 @@ public class JmsListeningServer extends AbsServer {
@Override @Override
public void run() { public void run() {
logger.info( LOG_FLAG + "JmsListeningServer is starting ." logger.info(LOG_FLAG + "JmsListeningServer is starting ."
+ LOG_FLAG); + LOG_FLAG);
ConnectionFactory connectionFactory; ConnectionFactory connectionFactory;
try { try {
...@@ -54,26 +54,34 @@ public class JmsListeningServer extends AbsServer { ...@@ -54,26 +54,34 @@ public class JmsListeningServer extends AbsServer {
Class<?> connectionFactoryClazz = Class Class<?> connectionFactoryClazz = Class
.forName(connectionFactoryClaz); .forName(connectionFactoryClaz);
connectionFactory = (ConnectionFactory) BeanUtils connectionFactory = (ConnectionFactory) BeanUtils
.instantiate(connectionFactoryClazz); .instantiateClass(connectionFactoryClazz);
BeanWrapper bw = new BeanWrapperImpl(connectionFactory); BeanWrapperImpl bw = new BeanWrapperImpl(connectionFactory);
Map<String, String> arguments = ((ServiceDef) this.serviceDef) Map<String, String> arguments = ((ServiceDef) this.serviceDef)
.getPropertyArguments("connectionFactory"); .getPropertyArguments("connectionFactory");
String username=null; String username = null;
String password=null; String password = null;
if (!arguments.isEmpty()) { if (!arguments.isEmpty()) {
// 连接工厂设置必要属性,动态设置 // 连接工厂设置必要属性,动态设置
for (Map.Entry<String, String> entry : arguments.entrySet()) { for (Map.Entry<String, String> entry : arguments.entrySet()) {
String propertyName = entry.getKey(); String propertyName = entry.getKey();
Object originalValue = entry.getValue(); Object originalValue = entry.getValue();
if("username".equalsIgnoreCase(propertyName)){ if ("username".equalsIgnoreCase(propertyName)) {
username=(String) originalValue; username = (String) originalValue;
}else if("password".equalsIgnoreCase(propertyName)){ } else if ("password".equalsIgnoreCase(propertyName)) {
password=(String) originalValue; password = (String) originalValue;
password=new String(Base64.decodeBase64(password)); password = new String(Base64.decodeBase64(password));
}else{ } else {
Object convertedValue = originalValue; Object convertedValue = originalValue;
convertedValue = ((BeanWrapperImpl) bw).convertForProperty(
convertedValue, propertyName); if ("hostName".equalsIgnoreCase(propertyName)) {
String ip = getServer(entry.getValue(), true);
convertedValue = bw.convertForProperty(ip, propertyName);
} else if ("port".equalsIgnoreCase(propertyName)) {
String port = getServer(entry.getValue(), false);
convertedValue = bw.convertForProperty(port, propertyName);
} else {
convertedValue = bw.convertForProperty(convertedValue, propertyName);
}
PropertyValue pv = new PropertyValue(propertyName, PropertyValue pv = new PropertyValue(propertyName,
convertedValue); convertedValue);
bw.setPropertyValue(pv); bw.setPropertyValue(pv);
...@@ -81,13 +89,13 @@ public class JmsListeningServer extends AbsServer { ...@@ -81,13 +89,13 @@ public class JmsListeningServer extends AbsServer {
} }
} }
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
if(username!=null){ if (username != null) {
UserCredentialsConnectionFactoryAdapter ucfa=new UserCredentialsConnectionFactoryAdapter(); UserCredentialsConnectionFactoryAdapter ucfa = new UserCredentialsConnectionFactoryAdapter();
ucfa.setTargetConnectionFactory(connectionFactory); ucfa.setTargetConnectionFactory(connectionFactory);
ucfa.setUsername(username); ucfa.setUsername(username);
ucfa.setPassword(password); ucfa.setPassword(password);
factory.setConnectionFactory(ucfa); factory.setConnectionFactory(ucfa);
}else{ } else {
factory.setConnectionFactory(connectionFactory); factory.setConnectionFactory(connectionFactory);
} }
factory.setMessageConverter(messageConverter); factory.setMessageConverter(messageConverter);
...@@ -116,8 +124,8 @@ public class JmsListeningServer extends AbsServer { ...@@ -116,8 +124,8 @@ public class JmsListeningServer extends AbsServer {
if (!StringUtil.isEmpty(receiveTimeout)) { if (!StringUtil.isEmpty(receiveTimeout)) {
factory.setReceiveTimeout(NumberUtils.toLong(receiveTimeout)); factory.setReceiveTimeout(NumberUtils.toLong(receiveTimeout));
} }
for (int i=0;i<destinations.length;i++) { for (int i = 0; i < destinations.length; i++) {
String destination=destinations[i]; String destination = destinations[i];
if (!containerCache.containsKey(destination)) { if (!containerCache.containsKey(destination)) {
// 设置监听端点 // 设置监听端点
SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint(); SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
...@@ -140,15 +148,31 @@ public class JmsListeningServer extends AbsServer { ...@@ -140,15 +148,31 @@ public class JmsListeningServer extends AbsServer {
} }
} catch (Throwable e) { } catch (Throwable e) {
logger.error( LOG_FLAG + "JmsListeningServer start error." logger.error(LOG_FLAG + "JmsListeningServer start error."
+ LOG_FLAG, e); + LOG_FLAG, e);
close(); close();
} }
logger.info( LOG_FLAG + "JmsListeningServer start is finished." logger.info(LOG_FLAG + "JmsListeningServer start is finished."
+ LOG_FLAG); + LOG_FLAG);
} }
private String getServer(String hostName, boolean isIp) {
if (hostName.indexOf(":") == -1) {
return hostName;
}
Objects.requireNonNull(hostName);
int indexOf = hostName.indexOf("-");
int index = 0;
String servers = hostName;
if (indexOf != -1) {
index = Integer.parseInt(hostName.substring(indexOf + 1));
servers = hostName.substring(0, indexOf);
}
String[] strings = servers.split(",");
return isIp ? strings[index].substring(0, strings[index].indexOf(":")) : strings[index].substring(strings[index].indexOf(":") + 1);
}
private void initDestinationType(DefaultJmsListenerContainerFactory factory) { private void initDestinationType(DefaultJmsListenerContainerFactory factory) {
String destinationType = getPropertyValue("destinationType"); String destinationType = getPropertyValue("destinationType");
boolean pubSubDomain = false; boolean pubSubDomain = false;
...@@ -184,8 +208,8 @@ public class JmsListeningServer extends AbsServer { ...@@ -184,8 +208,8 @@ public class JmsListeningServer extends AbsServer {
@Override @Override
public void close() { public void close() {
for(Map.Entry<String, DefaultMessageListenerContainer> item: containerCache.entrySet()){ for (Map.Entry<String, DefaultMessageListenerContainer> item : containerCache.entrySet()) {
DefaultMessageListenerContainer container=item.getValue(); DefaultMessageListenerContainer container = item.getValue();
if (container != null) { if (container != null) {
container.shutdown(); container.shutdown();
container = null; container = null;
...@@ -205,7 +229,7 @@ public class JmsListeningServer extends AbsServer { ...@@ -205,7 +229,7 @@ public class JmsListeningServer extends AbsServer {
public void handle(Object message) { public void handle(Object message) {
Client client = new Client(); Client client = new Client();
client.call(interfaceName, transactionName, new Object[] { message }); client.call(interfaceName, transactionName, new Object[]{message});
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment