applicationContext-task.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
default-lazy-init="true">
<!-- 作业调度框架 quartz 开始 -->
<!-- 要调用的工作类 -->
<bean id="buyTask" class="com.bingo.code.utils.task.BuyTask"></bean>
<!-- 定义调用对象和调用对象的方法 -->
<!-- 获取纳斯达克指数 -->
<bean id="timingUpdateBuyTask"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="buyTask" />
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>ssss</value>
</property>
</bean>
<!-- 定义触发时间 -->
<!-- 获取纳斯达克指数定时 -->
<bean id="updateBuy" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 指向我们的任务 -->
<property name="jobDetail">
<ref bean="timingUpdateBuyTask" />
</property>
<!-- 每天凌晨 5点半 触发? -->
<property name="cronExpression">
<!-- <value>30 * * * * ?</value> --> <!-- 调试用 每30秒触发一次 -->
<value>0 30 5 * * ?</value>
</property>
</bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="updateBuy"/><!-- 获取纳斯达克指数 -->
</list>
</property>
</bean>
<!-- 作业调度框架 quartz 结束 -->
</beans>
工作类
public void String ssss(){
String url =("https://hq.sinajs.cn/?_=0.4904364793823266&list=gb_ixic");
String list = ("list=gb_ixic");
String ssss = Utils.sendPost(url, list);
}
工具类
@Component
public final class Utils {
private static final Logger log = LoggerFactory.getLogger(Utils.class);
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
}
// 使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-49293.html