背景: WEBX -- 阿里巴巴网站所采用的WEB应用框架.
HSF接口: com.taobao.item.service.ItemQueryService
需求: 根据商品ID取得商品信息
载体: TBCMS系统
实现步骤: 1. 创建 /bundle/war/src/webroot/META-INF/autoconf/item-hsf-xml.vm
代码:
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
<bean id="itemQueryService" class="com.taobao.hsf.app.spring.util.HSFSpringConsumerBean" init-method="init">
<property name="interfaceName">
<value>com.taobao.item.service.ItemQueryService</value>
</property>
<property name="version">
<value>${item_provide_service_version}</value>
</property>
<!--asyncallMethods为可选配置[List],含义为调用此服务时需要采用异步调用的方法名列表,默认为空集合,即所有方法都采用同步调用-->
<property name="asyncallMethods">
<list>
</list>
</property>
</bean>
</beans>
2.其中: ${item_provide_service_version} 需要在 auto-config.xml 中配置,在 <group name="arkcms-bundle-war"> ...</group>中增加:
<property name="item.provide.service.version" defaultValue="1.0.0.daily" desc="itemClient调用版本"/>
同时在 <script>...</script>中增加:
<generate template="item-hsf.xml.vm" destfile="WEB-INF/biz/bean/item-hsf.xml" charset="GBK"/>
3.在 /bundle/war/src/webroot/WEB-INF/cms-biz-services.xml中增加:
<service name="BeanFactoryService" class="com.alibaba.service.spring.DefaultBeanFactoryService">
<property name="bean.descriptors">
....................
<value>/cms/biz/bean/item-hsf.xml</value>
</property>
</service>
至此,我们已经引入了ItemQueryService的HSF服务.
4. 接下来我们就要做Servlet接口了,当然写一个servlet直接调用hsf也是可以的,但是从结构考虑,我们还是先本地封装一层先.
但是首先,需要对 /all/project.xml 中需要增加对ItemQueryService的支持:
<projects name="taobao/itemcenter" version="1.9.5">
<project id="taobao/itemcenter/itemcenter-client"/>
<project id="taobao/itemcenter/itemcenter-common"/>
</projects>
并在/biz/project.xml和/web/project.xml中增加:
<?xml version="1.0" encoding="GB2312"?>
<project id="tbcms/web" extends="../all/project.xml">
<build>
<dependencies>
............
<include uri="taobao/itemcenter/itemcenter-client"/>
<include uri="taobao/itemcenter/itemcenter-common"/>
</dependencies>
</build>
</project>
命令行模式下执行:antx reactor goals=eclipse来执行对依赖包的装载.(前提是对/antx/repository.project/taobao/itemcenter/进行SVN更新)
本地封装: 在/biz/src/java/com/ark/cms/biz/util/item/下创建ItemService.java接口:
package com.ark.cms.biz.util.item;
import com.taobao.item.domain.DbRoute;
import com.taobao.item.domain.query.ItemIdDO;
import com.taobao.item.domain.result.ItemResultDO;
import com.taobao.item.exception.IcException;
/**
* 根据id取得商品信息
* @author duxing
*
*/
public interface ItemService {
public ItemResultDO getItemById(ItemIdDO itemId,DbRoute mainDbRoute) throws IcException;
}
在biz/src/java/com/ark/cms/biz/util/item/impl/下 ItemServiceImpl.java进行实现:
package com.ark.cms.biz.util.item.impl;
import com.ark.cms.biz.util.item.ItemService;
import com.taobao.item.domain.DbRoute;
import com.taobao.item.domain.query.ItemIdDO;
import com.taobao.item.domain.query.QueryItemOptionsDO;
import com.taobao.item.domain.result.ItemResultDO;
import com.taobao.item.exception.IcException;
import com.taobao.item.service.ItemQueryService;
/**
* 根据id取得商品信息 实现
* @author duxing
*
*/
public class ItemServiceImpl implements ItemService {
private ItemQueryService itemQueryService;
public void setItemQueryService(ItemQueryService itemQueryService) {
this.itemQueryService = itemQueryService;
}
@SuppressWarnings("deprecation")
public ItemResultDO getItemById(ItemIdDO itemId, DbRoute mainDbRoute) throws IcException {
QueryItemOptionsDO options = null;
return itemQueryService.queryItemById(itemId, options, mainDbRoute);
}
}
然后将此接口移交给spring,在 /bundle/war/src/webroot/META-INF/autoconf/item-hsf-xml.vm中增加
<bean id="itemService" class="com.ark.cms.biz.util.item.impl.ItemServiceImpl"></bean>
至此,我们已经在spring中产生一个itemService
5.接下来创建servlet
在/web/src/java/com/ark/cms/web/下创建ItemServiceServlet.java
因为servlet中不能直接用set形式让spring来set itemService进来,所以只能采用一下方法获取itemService:
BeanFactoryService beanFactory = (BeanFactoryService) BizServiceManager.getInstance().getService(BeanFactoryService.SERVICE_NAME);
ItemService itemService = (ItemService) beanFactory.getBean("itemService");
完整代码如下:
package com.ark.cms.web;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.service.spring.BeanFactoryService;
import com.ark.cms.biz.core.BizServiceManager;
import com.ark.cms.biz.util.item.ItemService;
import com.taobao.item.domain.DbRoute;
import com.taobao.item.domain.ItemDO;
import com.taobao.item.domain.query.ItemIdDO;
import com.taobao.item.domain.result.ItemResultDO;
import com.taobao.item.exception.IcException;
/**
* 根据id取得商品大图的接口
* @author duxing
*
*/
public class ItemServiceServlet extends HttpServlet {
private static final long serialVersionUID = -3348559907279438489L;
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// http://item.taobao.com/auction/item_detail-db1-67af640ba3f16a2006cd63f1f...
// long itemId=4399800173l;
String item_id = request.getParameter("item_id");
String xid = request.getParameter("xid");
DbRoute mainDbRoute=DbRoute.getDbRouteByXid(xid);
ItemIdDO itemIDDO=new ItemIdDO();
if(item_id!=null){
itemIDDO.setItemIdStr(item_id);
}
if(request.getParameter("item_num_id")!=null){
Long item_num_id = Long.valueOf(request.getParameter("item_num_id"));
itemIDDO.setItemId(item_num_id);
}
if(request.getParameter("id")!=null){
Long id = Long.valueOf(request.getParameter("id"));
itemIDDO.setItemId(id);
}
// 两种id的传入方式
// itemIDDO.setItemId(itemId);
// itemIDDO.setItemIdStr("67af640ba3f16a2006cd63f1f5dddc271");
ItemResultDO itemResultDO;
BeanFactoryService beanFactory = (BeanFactoryService) BizServiceManager.getInstance().getService(BeanFactoryService.SERVICE_NAME);
ItemService itemService = (ItemService) beanFactory.getBean("itemService");
try {
itemResultDO=itemService.getItemById(itemIDDO, mainDbRoute);
} catch (IcException e) {
e.printStackTrace();
itemResultDO=new ItemResultDO();
}
ItemDO itemDO=itemResultDO.getItem();
byte[] result="".getBytes();
if(itemDO!=null){
result=itemDO.getPictUrl().getBytes();
}
response.setContentType("text/xml;charset=gb2312");
response.setContentLength(result.length);
OutputStream out = response.getOutputStream();
out.write(result);
out.flush();
}
}
6.servlet写好了之后我们就要配置进WEBX了:
在 /bundle/war/src/webroot/META-INF/autoconf/web.xml.vm中增加:
<servlet-mapping>
<servlet-name>ItemServiceServlet</servlet-name>
<url-pattern>/item/query</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ItemServiceServlet</servlet-name>
<servlet-class>com.ark.cms.web.ItemServiceServlet</servlet-class>
</servlet>
7.至此,开发完成
命令行模式下执行:antx reactor goals=clean,default来对项目重新编译打包.重启本地服务器进行测试.
对/item/query接口进行post的时候触发此servlet.