maven pom:
<!-- RABBIT MQ -->
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-amqp</artifactId>
<version>1.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.1.4.RELEASE</version>
</dependency>
<!-- RABBIT MQ END -->
mq 主配置文件: spring-rabbit.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.1.xsd" >
<description>rabbitmq 连接服务配置</description>
<bean id="propertyConfigurerForRabbit"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="3"/>
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>file:/app/SFiles/ultron/install/rabbit.properties</value>
</list>
</property>
</bean>
<!-- 连接配置 --><!-- 172.30.16.93 -->
<rabbit:connection-factory id="connectionFactory"
addresses="${rabbit.addresses}"
username="${rabbit.user}"
password="${rabbit.pwd}" />
<rabbit:admin connection-factory="connectionFactory" />
<!-- spring template声明-->
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" message-converter="jsonMessageConverter" />
<!-- 消息对象json转换类 -->
<bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.JsonMessageConverter" />
<!-- 创建生产者 --><rabbit:queue id="send_coupon_by_scenario_queue" name="send_coupon_by_scenario_queue" durable="true" auto-delete="false" exclusive="false" />
<rabbit:queue id="open_account_queue" name="open_account_queue" durable="true" auto-delete="false" exclusive="false" />
<rabbit:direct-exchange name="direct-exchange" durable="true" auto-delete="false" id="direct-exchange">
<rabbit:bindings>
<!-- <rabbit:binding queue="send_coupon_single_queue" key="send_coupon_single_queue" /> -->
<rabbit:binding queue="send_coupon_by_scenario_queue" key="send_coupon_by_scenario_queue" />
<rabbit:binding queue="open_account_queue" key="open_account_queue" />
</rabbit:bindings>
</rabbit:direct-exchange>
<rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual" concurrency= "${listener.concurrency}">
<!-- 指定消费者 -->
<rabbit:listener queues="send_coupon_by_scenario_queue" ref="couponSendByScenarioListener" />
<rabbit:listener queues="open_account_queue" ref="acctOpenAccountMQListener" />
</rabbit:listener-container>
</beans>
rabbit.properties
rabbit.addresses=10.25.76.64:5672
rabbit.user=dev
rabbit.pwd=dev123
listener.concurrency=2
spring启动加载spring-rabbit.xml配置文件
<import resource="classpath:spring-rabbit.xml" />
具体实现 :
定义接口: Producer
package com.phzc.ubs.service.rabbit;
public interface Producer<M> {
void produce(String qKey, M m);
void setQueueName(String qName);
}
定义实体类 : OpenAccountMsg
package com.phzc.ubs.service.core.pasx.rabbit;
/**
* 开户信息 mq
*/
public class OpenAccountMsg {
private String custId;
private String custInfo;
private String acctName;
public String getAcctName() {
return acctName;
}
public void setAcctName(String acctName) {
this.acctName = acctName;
}
public String getCustId() {
return custId;
}
public void setCustId(String custId) {
this.custId = custId;
}
public String getCustInfo() {
return custInfo;
}
public void setCustInfo(String custInfo) {
this.custInfo = custInfo;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("OpenAccountMsg [");
if (custId != null)
builder.append("custId=").append(custId).append(", ");
if (custInfo != null)
builder.append("custInfo=").append(custInfo).append(", ");
if (acctName != null)
builder.append("acctName=").append(acctName);
builder.append("]");
return builder.toString();
}
}
定义实现 : OpenAccountProducer
package com.phzc.ubs.service.core.pasx.rabbit;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.phzc.ubs.service.rabbit.Producer;
@Component
public class OpenAccountProducer implements Producer<OpenAccountMsg>{
private static Logger logger = LoggerFactory.getLogger(OpenAccountProducer.class);
private static final String DEFAULT_QUEUE_NAME = "open_account_queue";
private String queueName = DEFAULT_QUEUE_NAME;
@Autowired
private AmqpTemplate amqpTemplate;
@Override
public void produce(String qKey, OpenAccountMsg msg) {
if (StringUtils.isEmpty(qKey)) {
qKey = queueName;
}
logger.info("开户消息:" + msg.toString());
try {
amqpTemplate.convertAndSend(qKey, msg);
} catch (Exception e) {
logger.error("开户异常", e);
}
}
@Override
public void setQueueName(String qName) {
this.queueName = qName;
}
}
定义消费者(Listener): 消费者处理队列内的消息
package com.phzc.ubs.service.core.pasx.rabbit;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
@Component
public class AcctOpenAccountMQListener implements ChannelAwareMessageListener {
private static final Logger logger = LoggerFactory.getLogger(AcctOpenAccountMQListener.class);
@Autowired
private AcctOpenAccountMQManager acctOpenAccountMQManager;
@Override
public void onMessage(Message message, Channel channel) throws Exception {
logger.info("=========UBS 开户接收消息:" + message.toString());
try {
String msgBody = new String(message.getBody());
OpenAccountMsg openAccountMsg = null;
if (StringUtils.isNotBlank(msgBody)) {
try {
openAccountMsg = JSON.parseObject(msgBody, OpenAccountMsg.class);
} catch (Exception e) {
logger.error("解析消息体出错: " + e.getMessage(), e);
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
return;
}
if (null != openAccountMsg) {
@SuppressWarnings("unused")
boolean openFlag = acctOpenAccountMQManager.openAccountForPasxUser(openAccountMsg);
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
}else {
logger.info("========解析对象出错=======");
throw new Exception("解析对象出错");
}
} else {
logger.info("=======开户消息为空=========");
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
}
} catch (Exception e) {
logger.info("======UBS调用ACCT开户处理异常,消息拒绝" + message.getMessageProperties().getDeliveryTag());
channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
}
}
}
定义辅助类:
package com.phzc.ubs.service.core.pasx.rabbit;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.phzc.acct.common.facade.enums.CapTypeEnum;
import com.phzc.acct.common.facade.enums.CuryTypeEnum;
import com.phzc.acct.common.facade.model.DebitAcctInfoAddRequest;
import com.phzc.acct.common.facade.model.DebitAcctInfoResult;
import com.phzc.ubs.common.constants.CommonConstants;
import com.phzc.ubs.common.constants.RespCode;
import com.phzc.ubs.common.dal.model.UsrAcctList;
import com.phzc.ubs.common.facade.enums.AcctTypeEnum;
import com.phzc.ubs.common.integration.acct.DebitAcctInfoClient;
import com.phzc.ubs.service.core.usracctlist.UsrAcctListService;
import com.phzc.ultron.common.constants.code.systemCode.SysCode;
import com.phzc.ultron.runtime.context.UltronApplicationContext;
@Component
public class AcctOpenAccountMQManager {
private static final Logger logger = LoggerFactory.getLogger(AcctOpenAccountMQManager.class);
@Autowired
private DebitAcctInfoClient debitAcctInfoClient;
@Autowired
private UsrAcctListService usrAcctListService;
public boolean openAccountForPasxUser(OpenAccountMsg openAccountMsg) throws Exception {
String custId = openAccountMsg.getCustId();
String custInfo = openAccountMsg.getCustInfo();
String acctName = openAccountMsg.getAcctName();
logger.info("============= 本次开户开始=========== custId = "+custId);
DebitAcctInfoAddRequest acctAddRequest = newOneDebitAcctInfoAddRequest(custId, custInfo, acctName);
logger.info("=========UBS调用acct开户接口 上送参数 = " + JSON.toJSONString(acctAddRequest));
DebitAcctInfoResult openAccountResult = debitAcctInfoClient.addDebitAcctInfo(acctAddRequest);
logger.info("=========UBS调用acct开户接口 响应 = " + JSON.toJSONString(acctAddRequest));
if(null!=openAccountResult && RespCode.SUCCESS.getReturnCode().equals(openAccountResult.getRespCode())) {
// 创建用户子账号。
UsrAcctList usrAcctList = new UsrAcctList();
usrAcctList.setCustId(custId);
usrAcctList.setSubAcctId(AcctTypeEnum.BASEDA.getCode());
usrAcctList.setAcctType(AcctTypeEnum.BASEDA.getCode());
usrAcctList.setDcFlag("D");
usrAcctList.setAcctAlias("def");
usrAcctList.setStat("N");
int count = usrAcctListService.insertAccountInfo(usrAcctList);
if (count < 1) {
logger.error("=========创建用户子账号 失败=========");
}
logger.info("============= 本次开户正常结束=========== custId = "+custId);
} else {
logger.error("=========UBS调用acct开户失败==========");
throw new Exception("UBS调用acct开户失败");
}
return true;
}
/**
* 创建 开户调用ACCT的对象
*/
private DebitAcctInfoAddRequest newOneDebitAcctInfoAddRequest(String custId, String custInfo, String acctName) {
DebitAcctInfoAddRequest acctAddRequest = new DebitAcctInfoAddRequest();
acctAddRequest.setCustId(custId);
acctAddRequest.setSubAcctId(AcctTypeEnum.BASEDA.getCode());
acctAddRequest.setCapType(CapTypeEnum.CS.getCode());// 资金类型
acctAddRequest.setCuryType(CuryTypeEnum.CNY.getCode());// 币种
acctAddRequest.setAcctType(AcctTypeEnum.BASEDA.getCode());
acctAddRequest.setAcctName(acctName);
acctAddRequest.setCustInfo(custInfo);
acctAddRequest.setSysId(SysCode.SYS_60033.getCode());
acctAddRequest.setBdepId("phzc");// 业务部门代号-枚举
acctAddRequest.setChkValue(getConfigValue(CommonConstants.GLOBAL_ACCT_MD5_KEY));
return acctAddRequest;
}
/**
* 获取配置
*
* @param key
* @return
*/
public static String getConfigValue(String key) {
String value = (String) UltronApplicationContext.getApplicationContext(key);
if (StringUtils.isBlank(value)) {
logger.info("======获取配置失败:key = " + key);
return null;
}
return value;
}
}
代码业务逻辑 : (生产者生产消息)
@Autowired
private OpenAccountProducer openAccountProducer;
OpenAccountMsg openAccountMsg = new OpenAccountMsg();
openAccountMsg.setCustId("2018051811250001");
openAccountMsg.setCustInfo("oracle");
openAccountMsg.setAcctName("scott");
//开户 -- 消息生产
openAccountProducer.produce(null, openAccountMsg);
结束!!!