近期在给公司项目做二次重构,将原来庞大的系统拆分成几个小系统。系统与系统之间通过接口调用,系统间通信有非常多方式,如系统间通信接口做成请求controller,只是这样不方便也不安全,经常使用的方式是使用rpc技术,能够使用webservices技术等等。因为我的架构是使用spring,并且spring在集成这块做的非常不错。如hessian,blurp,webservices。httpinvoke,rmi,jms等,我这里採用的是jms技术。

废话不多说间代码。

场景描写叙述:A系统须要调用B系统提供的接口

1、B系统提供的接口

a、创建接口

package com.maimai.test.jmsservice;

import com.maimai.db_bean.User;

public interface AlertService {

	public String sendStr(String str);

	public void print_r(String str);

	public User findById(long id);

}

b、创建接口实现类

package com.maimai.test.jmsservice;

import org.springframework.beans.factory.annotation.Autowired;

import com.maimai.dao.UserDao;
import com.maimai.db_bean.User; public class AlertServiceImpl implements AlertService {
@Autowired
private UserDao userDao;
public String sendStr(final String str) { System.out.println("========收到的信息======"+str+"================");
return "来自服务端的返回信息,我接收到数据了 ...";
} public void print_r(String str){
System.out.println("========收到的信息======"+str+"================");
} public User findById(long id) {
User user = userDao.findById(id);
return user;
} }

c、配置amq和jms

amq配置

<amq:connectionFactory id="connectionFactory"
brokerURL="tcp://localhost:61616" />
<amq:queue id="queue" physicalName="mail.queue"></amq:queue>

jms配置

<jms:listener-container connection-factory="connectionFactory">
<jms:listener destination="mail.queue" ref="jmsreciever" method="getStr"/>
<jms:listener destination="couponMessage.queue" ref="jsmRecieveMessage" method="receiveJMSMessage"/>
<jms:listener destination="spitter.alert.queue" ref="alertServiceExporter"/>
</jms:listener-container>

//备注这里仅仅须要destination="spitter.alert.queue"

d、将接口导出成jms服务配置

<bean id="alertServiceExporter"
class="org.springframework.jms.remoting.JmsInvokerServiceExporter"
p:service-ref="alertService"
p:serviceInterface="com.maimai.test.jmsservice.AlertService"/>
<bean id="alertService" class="com.maimai.test.jmsservice.AlertServiceImpl"/>

备注:此刻B系统提供接口准备工作做完了,接下来是A系统的工作

2、A系统调用B系统提供的服务

a、将B系统中接口所在包拷贝到A系统中。或者将B系统接口打包jar导入到A系统中

备注:仅仅须要将com.maimai.test.jmsservice.AlertService这个复制过来就可以,不须要实现类

b、在A系统中配置amq

配置amq

<amq:connectionFactory id="connectionFactory"
brokerURL="tcp://localhost:61616" />
<amq:queue id="alertServiceQueue" physicalName="spitter.alert.queue"></amq:queue>

配置jms工厂

<bean id="alertService"
class="org.springframework.jms.remoting.JmsInvokerProxyFactoryBean" >
<property name="connectionFactory" ref="connectionFactory" />
<property name="queueName" value="spitter.alert.queue" />
<property name="serviceInterface" value="com.maimai.test.jmsservice.AlertService"></property>
</bean>

备注:到此,我们都已经配置好了,接下来是A系统開始调用B系统提供的接口服务

package com.maimai.action;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired; import com.maimai.db_bean.Product;
import com.maimai.db_bean.ProductBrand;
import com.maimai.db_bean.ProductCategory;
import com.maimai.db_bean.ProductCommentRecord;
import com.maimai.db_bean.ProductDetail;
import com.maimai.db_bean.ProductLimitedTime;
import com.maimai.db_bean.ProductType;
import com.maimai.db_bean.ScannedProduct;
import com.maimai.db_bean.User;
import com.maimai.db_bean.HotProduct;
import com.maimai.db_bean.UserShop;
import com.maimai.engine.AfterSaleEngine;
import com.maimai.engine.DealEngine;
import com.maimai.engine.ProductEngine;
import com.maimai.engine.UserEngine;
import com.maimai.service.UserShopService;
import com.maimai.test.jmsservice.AlertService;
import com.maimai.util.Constant;
import com.maimai.util.IKAnalyzerUtil;
import com.maimai.util.Util;
import com.opensymphony.xwork2.ActionSupport; /**
* 商品Action
*
*/
public class ProductAction extends ActionSupport implements ServletRequestAware {
/**
* 序列号
*/
private static final long serialVersionUID = 495219298210322438L; private HttpServletRequest request; // 商品engine
@Autowired
private ProductEngine productEngine; // 用户engine
@Autowired
private UserEngine userEngine; // 交易Engine
@Autowired
private DealEngine dealEngine; // 售后服务Engine
@Autowired
private AfterSaleEngine afterSaleEngine; @Autowired
private UserShopService userShopService; @Autowired
AlertService alertService; /************
* 进入商品具体页
* */
public String ToProductDetail(){
// 获取商品id
String str = this.alertService.sendStr("你好,中国"); //调用B系统接口
System.out.println("=================="+str+"====================");
this.alertService.print_r("你好,哈哈哈。成功了!!!!"); <span style="font-family: Arial, Helvetica, sans-serif;">//调用B系统接口</span>
User user1 = this.alertService.findById(10); <span style="font-family: Arial, Helvetica, sans-serif;">//调用B系统接口</span>
System.out.println(user1.toString()); long productId = Long.parseLong(this.request.getParameter("pid"));
// 依据id获取商品
Product product = this.productEngine.getProductById(productId);
if (Util.isNullOrEmpty(product)) {
return "ProductError";
}
this.request.setAttribute("product", product);
//added by sam 店铺查询
UserShop userShop = new UserShop();
if(product.getUserId() != 0 ){
userShop = this.userShopService.findByUserId(product.getUserId());
}
this.request.setAttribute("userShop", userShop);
//ended by sam // 依据商品所属分类id获取商品列表
List<Product> sameCategoryProducts = this.productEngine.getProductsByCategoryId(product.getCategoryId(), 1, 5);
this.request.setAttribute("sameCategoryProducts", sameCategoryProducts); /** 记录浏览了此商品 **/
try {
// 获取当前登录用户
User user = this.userEngine.getCurrentuser();
ScannedProduct scannedProduct = new ScannedProduct();
scannedProduct.setProductId(productId);
scannedProduct.setUserId(user.getId());
this.productEngine.getProductService().saveScannedProduct(scannedProduct);
} catch (Exception e) {
//e.printStackTrace();
}
return "ToProductDetail";
} }

A系统调用B系统成功信息例如以下:

========收到的信息======你好,中国================

2015-12-31 09:35:52,261 [org.springframework.jms.listener.DefaultMessageListenerContainer#2-2] DEBUG [org.springframework.remoting.support.RemoteInvocationTraceInterceptor] - Finished processing of JmsInvokerServiceExporter remote call: com.maimai.test.jmsservice.AlertService.sendStr

 ==================来自服务端的返回信息。我接收到数据了 ...====================

2015-12-31 09:35:52,332 [org.springframework.jms.listener.DefaultMessageListenerContainer#2-2] DEBUG [org.springframework.jms.listener.DefaultMessageListenerContainer] - Received message of type [class org.apache.activemq.command.ActiveMQObjectMessage] from
consumer [ActiveMQMessageConsumer { value=ID:PC-20150906SEWA-53438-1451525683864-0:21:1:1, started=true }] of session [ActiveMQSession {id=ID:PC-20150906SEWA-53438-1451525683864-0:21:1,started=true}]

 2015-12-31 09:35:52,333 [org.springframework.jms.listener.DefaultMessageListenerContainer#2-2] DEBUG [org.springframework.remoting.support.RemoteInvocationTraceInterceptor] - Incoming JmsInvokerServiceExporter remote call: com.maimai.test.jmsservice.AlertService.print_r

 ========收到的信息======你好,哈哈哈,成功了!!!!================

2015-12-31 09:35:52,606 [org.springframework.jms.listener.DefaultMessageListenerContainer#2-2] DEBUG [org.springframework.remoting.support.RemoteInvocationTraceInterceptor] - Finished processing of JmsInvokerServiceExporter remote call: com.maimai.test.jmsservice.AlertService.findById

 User [accountImage=sysImg/user/10/account/account20150810170819.jpg, age=12, email=526713869@qq.com, idCode=34081119, idImageBack=sysImg/user/id/11510411712110199104971114953514853535448575448/11510411712110199104971114953514853535448575448Back.jpg, idImageFore=sysImg/user/id/11510411712110199104971114953514853535448575448/11510411712110199104971114953514853535448575448Fore.png,
isLocked=0, loginName=samtest, password=`O怾,&#6;u?

攠聉?, qq=526713869, realName=张三, registTime=2015-01-26 09:56:04, sex=f, userType=0, telNum=15305560960, status=0]

JMS解决系统间通信问题的更多相关文章

  1. 系统间通信(10)——RPC的基本概念

    1.概述 经过了详细的信息格式.网络IO模型的讲解,并且通过JAVA RMI的讲解进行了预热.从这篇文章开始我们将进入这个系列博文的另一个重点知识体系的讲解:RPC.在后续的几篇文章中,我们首先讲解R ...

  2. 系统间通信(5)——IO通信模型和JAVA实践 下篇

    7.异步IO 上面两篇文章中,我们分别讲解了阻塞式同步IO.非阻塞式同步IO.多路复用IO 这三种IO模型,以及JAVA对于这三种IO模型的支持.重点说明了IO模型是由操作系统提供支持,且这三种IO模 ...

  3. 系统间通信(3)——IO通信模型和JAVA实践 上篇

    来源:http://blog.csdn.net/yinwenjie 1.全文提要 系统间通信本来是一个很大的概念,我们首先重通信模型开始讲解.在理解了四种通信模型的工作特点和区别后,对于我们后文介绍搭 ...

  4. WebService与RMI(远程调用方式实现系统间通信)

    前言 本文是<分布式java应用基础与实践>读书笔记:另外参考了此博客,感觉讲的挺好的,尤其是其中如下内容: 另外,消息方式实现系统间通信本文不涉及.RMI则只采用spring RMI框架 ...

  5. 系统间通信——RPC架构设计

    架构设计:系统间通信(10)——RPC的基本概念 1.概述经过了详细的信息格式.网络IO模型的讲解,并且通过JAVA RMI的讲解进行了预热.从这篇文章开始我们将进入这个系列博文的另一个重点知识体系的 ...

  6. 架构设计:系统间通信(20)——MQ:消息协议(下)

    (接上文<架构设计:系统间通信(19)--MQ:消息协议(上)>) 上篇文章中我们重点讨论了"协议"的重要性.并为各位读者介绍了Stomp协议和XMPP协议. 这两种协 ...

  7. 架构设计:系统间通信(34)——被神化的ESB(上)

    1.概述 从本篇文章开始,我们将花一到两篇的篇幅介绍ESB(企业服务总线)技术的基本概念,为读者们理清多个和ESB技术有关名词.我们还将在其中为读者阐述什么情况下应该使用ESB技术.接下来,为了加深读 ...

  8. 系统间通信(9)——通信管理与RMI 下篇

    接上文<架构设计:系统间通信(8)--通信管理与RMI 上篇>.之前说过,JDK中的RMI框架在JDK1.1.JDK1.2.JDK1.5.JDK1.6+几个版本中做了较大的调整.以下我们讨 ...

  9. 系统间通信(8)——通信管理与RMI 上篇

    1.概述 在概述了数据描述格式的基本知识.IO通信模型的基本知识后.我们终于可以进入这个系列博文的重点:系统间通信管理.在这个章节我将通过对RMI的详细介绍,引出一个重要的系统间通信的管理规范RPC, ...

随机推荐

  1. cookie和seesion区别

    cookie 和session 的区别详解 这些都是基础知识,不过有必要做深入了解.先简单介绍一下. 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie ...

  2. 题解报告:hdu 1272 小希的迷宫

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem ...

  3. PHP 在线 编辑 解析

    http://www.w3schools.com/php/default.asp    http://www.w3schools.com/php/showphp.asp?filename=demo_s ...

  4. Android 控制硬加速 hardwareAccelerated

    从Android3.0 (API level11)开始,Android的2D显示管道被被设计得更加支持硬加速了.硬加速使用GPU承担了所有在View的canvas上执行的绘制操作. 启用硬加速最简单的 ...

  5. 控制台——对WIN32 API的使用(user32.dll)

    Win32 API概念:即为Microsoft 32位平台的应用程序编程接口(Application Programming Interface).所有在Win32平台上运行的应用程序都可以调用这些函 ...

  6. iframe监听unload事件

    阻止默认事件 event.preventDefault(); 阻止事件冒泡 event.stopPropagation(); event.cancelBubble = true; //IE <a ...

  7. 06--ubuntu的sqlite安装

    知道学习嵌入式技术,数据库是必须懂的,看的书上嵌入式的教程都在用,看来我是非学不可了,下面就简单的记录一下我在Ubuntu 12.04系统上安装 SQLite 的过程以及使用. 相关阅读: SQLit ...

  8. DateTimePicker 控件置空

    dtOrderDateFrom.Format = DateTimePickerFormat.Custom; dtOrderDateFrom.CustomFormat = " "; ...

  9. webstorm_completion

    js 使用yarn 安装声明定义文件 @types/xxx koa ==> @types/koa koa-router ==> @types/koa-router 安装webstorm中的 ...

  10. C lstat major MAJOR 获得设备号

    #cat lstat.c #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #inc ...