WriteFundXml.java
public class WriteFundXml { private static Log logger = LogFactory.getLog(WriteFundXml.class); private IndexNetValue indexNetValue; public void execute() { System.out.println("执行咯"); //从数据库中取得结果集 List<TaFundDay> fundList = null; fundList = indexNetValue.getTaFundDay(); try { XMLWriter writer = null;// 声明写XML的对象 OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("GBK");// 设置XML文件的编码格式 String filePath = "d://net_value.xml";//改成相对路径 File file = new File(filePath); Document document = DocumentHelper.createDocument(); Element root = document.addElement("Root"); Iterator<TaFundDay> fundIter = fundList.iterator(); while(fundIter.hasNext()) { TaFundDay taFundDay = fundIter.next(); Element fund = root.addElement("Fund"); fund.addAttribute("code", taFundDay.getC_fundCode()); Element date = fund.addElement("Date"); date.setText(taFundDay.getD_cDate().toString()); Element fundName = fund.addElement("Fund-Name"); fundName.setText(taFundDay.getC_fundName()); Element netValue = fund.addElement("Net-Value"); netValue.setText(String.valueOf(taFundDay.getF_netValue())); Element totalNetValue = fund.addElement("Total-Net-Value"); totalNetValue.setText(String.valueOf(taFundDay.getF_totalNetValue())); } writer = new XMLWriter(new FileWriter(file), format); writer.write(document); writer.close(); } catch(IOException e) { logger.error("com.syjz.util.WriteFundXml ======>> IOException"); } catch(Exception e) { logger.error("com.syjz.util.WriteFundXml ======>> Exception"); } } public IndexNetValue getIndexNetValue() { return indexNetValue; } public void setIndexNetValue(IndexNetValue indexNetValue) { this.indexNetValue = indexNetValue; } }
ReadFundXml.java
public class ReadFundXml { public String getIndexFundStream() { SAXReader reader = new SAXReader(); Document document = null; try { document = reader.read(new File("d://net_value.xml")); } catch (DocumentException e) { e.printStackTrace(); } Element root=document.getRootElement(); String rootXmlText=root.asXML(); //String docXmlText = document.asXML(); 带XML头 return rootXmlText; } }
getIndexFund.jsp
<%@ page language="java" pageEncoding="utf-8"%> <%@ page import="com.syjz.util.ReadFundXml" %> <% response.setContentType("text/xml; charset=utf-8"); response.setHeader("Cache-Control", "no-cache"); ReadFundXml ReadFundXml = new ReadFundXml(); String xmlString = ReadFundXml.getIndexFundStream(); out.println(xmlString); %>
index.html
<mce:script type="text/javascript"><!-- var XMLHttpReq=false; function createXMLHttpRequest() { //创建xmlhttprequest; if(window.XMLHttpRequest) { //Mozilla 浏览器 XMLHttpReq = new XMLHttpRequest(); } else if (window.ActiveXObject) { // IE浏览器 try { XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); } } } function getIndexFund(){ createXMLHttpRequest(); var url="./getIndexFund.jsp"; XMLHttpReq.onreadystatechange = processlogin ; XMLHttpReq.open("GET",url,true); XMLHttpReq.send(null); } function processlogin(){ if (XMLHttpReq.readyState == 4) {// 判断对象状态 if (XMLHttpReq.status == 200) { // 信息已经成功返回,开始处理信息 var xmlDoc=XMLHttpReq.responseXML; //用于取得基金的个数 var xmlDoc_num = xmlDoc.getElementsByTagName("Fund"); var xmlDoc_date = xmlDoc.getElementsByTagName("Date"); var xmlDoc_fundName = xmlDoc.getElementsByTagName("Fund-Name"); var xmlDoc_netValue = xmlDoc.getElementsByTagName("Net-Value"); var xmlDoc_totalNetValue = xmlDoc.getElementsByTagName("Total-Net-Value"); var netValueList = ''; for(var i=0; i<xmlDoc_num.length; i++) { netValueList = netValueList + "<tr><td><img src="images/arrUp.gif" mce_src="images/arrUp.gif"/></td><td>" + xmlDoc_date[i].childNodes[0].nodeValue + "</td>" + "<td>" + xmlDoc_fundName[i].childNodes[0].nodeValue + "</td>" + "<td>" + xmlDoc_netValue[i].childNodes[0].nodeValue + "</td>" + "<td>" + xmlDoc_totalNetValue[i].childNodes[0].nodeValue + "</td>" + "<td>-</td><td>-</td><td>-</td><td>-</td><td>平衡</td><td>暂停</td><td><img src="images/btnBuy.gif" mce_src="images/btnBuy.gif"/></td>" + "</tr>"; } var netShowList_1 = document.getElementById('netShowList_1'); netShowList_1.innerHTML = "<table width='100%' border='0' cellspacing='0' cellpadding='0' class='netShowTable'>" + "<tr><th>1</th><th>净值日期</th><th>基金名称</th><th>单位净值</th><th>累计净值</th><th>日涨跌</th><th>最近半年</th><th>最近一年</th><th>成立以来</th><th>类型</th><th>状态</th><th>买入</th></tr>" + netValueList + "<tr><th height='3' colspan='12'></th>" + "</tr>" + "</table>"; } else { //页面不正常 //window.alert("您所请求的页面有异常。"); } } } // --></mce:script>
applicationContext-quartz.xml
</beans> <!-- quartz --> <!-- 定时服务定义 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- 自动启动 --> <property name="autoStartup"> <value>true</value> </property> <property name="triggers"> <list> <!-- 处理过期问题的定时任务 --> <ref local="assessHastenTaskTrigger"/> <!-- 生成首页净值数据的XML的定时任务 --> <ref local="indexNetValueTaskTrigger"/> </list> </property> </bean> <!-- 定时器定义 --> <bean id="assessHastenTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="assessHastenJobDetail"/> </property> <property name="cronExpression"> <!-- 每30分钟运行一次 --> <value>0 1/30 * * * ?</value> </property> </bean> <!-- JobDetail定义 --> <bean id="assessHastenJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="assessHastenJob" /> </property> <property name="targetMethod"> <value>execute</value> </property> <property name="concurrent" value="false" /> </bean> <!-- Job定义 --> <bean id="assessHastenJob" class="com.mgzst.util.QuartzJob"> <property name="iquartzdao" ref="iquartzdaoimpl"/> <property name="iquestiondao" ref="iquestiondaoimpl"/> <property name="isolutiondao" ref="isolutiondaoimpl"/> </bean> <!-- ========================== --> <!-- 生成首页净值XML数据的 定时器定义 --> <bean id="indexNetValueTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="indexNetValueJobDetail"/> </property> <property name="cronExpression"> <!-- 每30分钟运行一次 --> <value>0 1/30 * * * ?</value> </property> </bean> <!-- 生成首页净值XML数据的 JobDetail定义 --> <bean id="indexNetValueJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="indexNetValueJob" /> </property> <property name="targetMethod"> <value>execute</value> </property> <property name="concurrent" value="false" /> </bean> <!-- 生成首页净值XML数据的 Job定义 --> <bean id="indexNetValueJob" class="com.syjz.util.WriteFundXml"> <property name="indexNetValue"> <ref bean="indexNetValue"/> </property> </bean> <!-- datasource --> <bean id="indexNetValue" class="com.syjz.dao.IndexNetValue" > <property name="jdbcTemplateTA"> <ref bean="jdbcTemplateTA"/> </property> </bean> <!-- ta 数据源 以及TAJdbcTemplate--> <bean id="dataSourceTA" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"> </property> <property name="jdbcUrl" value="jdbc:oracle:thin:@ip:1521:oranb"> </property> <property name="user" value="***"></property> <property name="password" value="***"></property> <!-- 连接池中保留的最小连接数。 --> <property name="minPoolSize"> <value>1</value> </property> <!-- 连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize"> <value>10</value> </property> <!-- 初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize"> <value>10</value> </property> <!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime"> <value>60</value> </property> <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement"> <value>5</value> </property> <!-- JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。 如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 --> <property name="maxStatements"> <value>0</value> </property> <!-- 每60秒检查所有连接池中的空闲连接。Default: 0 --> <property name="idleConnectionTestPeriod"> <value>60</value> </property> <!-- 定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 --> <property name="acquireRetryAttempts"> <value>30</value> </property> <!-- 获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试 获取连接失败后该数据源将申明已断开并永久关闭。Default: false --> <property name="breakAfterAcquireFailure"> <value>true</value> </property> <!-- 因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable 等方法来提升连接测试的性能。Default: false --> <property name="testConnectionOnCheckout"> <value>false</value> </property> </bean> <bean id="jdbcTemplateTA" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref bean="dataSourceTA" /> </property> </bean> <!-- ta 数据源 end --> </beans>