提交 248b57b2 编写于 作者: shuzheng5201314's avatar shuzheng5201314

初始化notify

上级 e9b08db4
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zheng</groupId>
<artifactId>zheng-notify</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>zheng-notify</name>
<url>http://www.zhangshuzheng.cn</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
</properties>
<modules>
<module>zheng-notify-sdk</module>
<module>zheng-notify-server</module>
</modules>
</project>
<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>管理监控中心</title>
<style>
#main{width:100%;height:400px;margin:0 auto;}
.namespace{width:100%;height:400px;background:#000;}
.title{width:100%;height:40px;font-size:0;background:#666;}
.input,.button{float:left;height:40px;border:none;background:none;outline:none;color:#fff;}
.input{width:25%;text-indent:10px;font-weight:bold;}
.button{width:10%;cursor:pointer;}
#token{width:20%;}
#env{background:rgb(194, 53, 49);}
.messages{width:100%;height:340px;overflow-y:auto;font-size:12px;line-height:2em;color:#fff;}
.messages li{list-style-type:none;}
.messages .connect{color:#00cc00;}
.messages .warn{color:#cccc00;}
.messages .disconnect{color:#cc0000;}
table{width:100%;border-collapse:collapse;border:solid #ccc;border-width:1px 0 0 1px}
table th,table td{border:solid #ccc;border-width:0 1px 1px 0;padding:14px;text-align:center}
</style>
</head>
<body>
<div id="main">
<div class="namespace">
<div class="title">
<select id="env" class="input">
<option value="http://localhost:8882">开发环境</option>
</select>
<input id="namespace" class="input" type="text" placeholder="namespace" value=""/>
<input id="token" class="input" type="text" placeholder="token" value=""/>
<input id="connect" class="button" type="button" value="连接"/>
<input id="send" class="button" type="button" value="发言"/>
<input id="disconnect" class="button" type="button" value="断开"/>
</div>
<div class="messages">
<ul id="list">
</ul>
</div>
</div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/socket.io/2.1.1/socket.io.js"></script>
<script type="text/javascript">
$(function() {
// SocketIO客户端
var socket;
$('#connect').click(function() {
// 获取token,请求业务系统接口
var token = $('#token').val();
// 创建socketio连接
socket = io.connect($('#env').val() + $('#namespace').val() + '?token=' + token);
socket.on('connect', function() {
$('#list').prepend('<li class="connect">已连接服务器!</li>');
});
socket.on('message', function(data, ackServerCallback) {
$('#list').prepend('<li class="data">收到服务器消息:' + JSON.stringify(data) + '</li>');
if (ackServerCallback) {
ackServerCallback();
}
});
socket.on('connect_error', function(error) {
$('#list').prepend('<li class="warn">连接服务器失败!</li>');
console.log(error);
});
socket.on('disconnect', function() {
$('#list').prepend('<li class="disconnect">已断开服务器!</li>');
});
$('#send').click(function() {
socket.emit('message', {userName: 'userName', message: new Date()});
});
$('#disconnect').click(function() {
socket.close();
});
// 防止重复连接
$(this).attr('disabled','disabled');
});
});
</script>
</body>
</html>
\ No newline at end of file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zheng</groupId>
<artifactId>zheng-notify</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>zheng-notify-sdk</artifactId>
<packaging>jar</packaging>
<name>zheng-notify-sdk</name>
<url>http://www.zhangshuzheng.cn</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
</dependencies>
<build>
<finalName>zheng-notify-sdk</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.zheng.notify.sdk;
/**
* 统一返回结果类
* Created by shuzheng on 2017/2/18.
*/
public class BaseResult {
/**
* 状态码:1成功,其他为失败
*/
public int code;
/**
* 成功为success,其他为失败原因
*/
public String message;
/**
* 数据结果集
*/
public Object data;
public BaseResult(int code, String message, Object data) {
this.code = code;
this.message = message;
this.data = data;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
package com.zheng.notify.sdk;
/**
* 全局常量
* Created by shuzheng on 2018/6/28.
*/
public class NotifyConstants {
// 通知类型:广播
public static final String PUBLISH_TYPE_BROADCAST = "broadcast";
// 通知类型:主题
public static final String PUBLISH_TYPE_TOPICS = "topics";
// 通知类型:点对点
public static final String PUBLISH_TYPE_CLIENTS = "clients";
}
package com.zheng.notify.sdk;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.nio.charset.Charset;
/**
* 推送工具类
* Created by shuzheng on 2018/6/28.
*/
public class NotifyHelper {
/**
* 推送消息
* @param url
* @param publishDto
* @return
*/
public BaseResult publish(String url, PublishDto publishDto) {
HttpPost httpPost = null;
try {
HttpClient httpClient = new DefaultHttpClient();
httpPost = new HttpPost(url);
httpPost.setHeader("Content-type", "application/json; charset=utf-8");
HttpEntity httpEntity = new StringEntity(JSONObject.toJSONString(publishDto), "utf-8");
httpPost.setEntity(httpEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity resEntity = httpResponse.getEntity();
if (resEntity != null) {
String result = EntityUtils.toString(resEntity, Charset.forName("utf-8"));
return JSONObject.parseObject(result, BaseResult.class);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpPost != null) {
httpPost.releaseConnection();
}
}
return new BaseResult(-1, "error", "publish error");
}
public static void main(String[] args) {
String url = "http://localhost:8881/notify/publish";
PublishDto publishDto = new PublishDto();
publishDto.setType(NotifyConstants.PUBLISH_TYPE_BROADCAST);
publishDto.setNamespace("/demo");
publishDto.setAccessKey("1");
publishDto.setPayload("hi");
BaseResult result = new NotifyHelper().publish(url, publishDto);
System.out.println(result.message);
}
}
package com.zheng.notify.sdk;
/**
* 推送消息
* Created by shuzheng on 2018/6/20.
*/
public class PublishDto {
// 推送类型:"broadcast/topics/clients" <=> 广播、按主题、点对点
private String type;
// 推送命名空间
private String namespace;
// 鉴权key
private String accessKey;
// 推送订阅主题的订阅者
private String[] topics;
// 推送客户端
private String[] clients;
// 消息体
private Object payload;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getNamespace() {
return namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
public String[] getTopics() {
return topics;
}
public void setTopics(String[] topics) {
this.topics = topics;
}
public String[] getClients() {
return clients;
}
public void setClients(String[] clients) {
this.clients = clients;
}
public Object getPayload() {
return payload;
}
public void setPayload(Object payload) {
this.payload = payload;
}
public String getAccessKey() {
return accessKey;
}
public void setAccessKey(String accessKey) {
this.accessKey = accessKey;
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zheng</groupId>
<artifactId>zheng-notify</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>zheng-notify-server</artifactId>
<packaging>jar</packaging>
<name>zheng-notify-server</name>
<url>http://www.zhangshuzheng.cn</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.zheng</groupId>
<artifactId>zheng-common</artifactId>
<version>1.0.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
<build>
<finalName>zheng-notify-server</finalName>
<filters>
<filter>src/main/resources/profiles/${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<!-- jetty插件 -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<!--<version>9.0.0.v20130308</version>-->
<version>9.2.7.v20150116</version>
<configuration>
<scanIntervalSeconds>3</scanIntervalSeconds>
<webApp>
<contextPath>/</contextPath>
</webApp>
<httpConnector>
<port>8881</port>
</httpConnector>
<reload>automatic</reload>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.zheng.notify.server.controller;
import com.zheng.common.base.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 测试controller
* Created by shuzheng on 2017/2/18.
*/
@Controller
public class IndexController extends BaseController {
private static final Logger LOGGER = LoggerFactory.getLogger(IndexController.class);
@RequestMapping(value = "/index", method = RequestMethod.GET)
@ResponseBody
public String index() {
return "index";
}
}
\ No newline at end of file
app.name=${app.name}
env=${profile.env}
zheng.ui.path=${zheng.ui.path}
\ No newline at end of file
403=Access forbidden!
404=Page not found!
500=Internal server error!
error=Server error!
\ No newline at end of file
403=\u6CA1\u6709\u6743\u9650\uFF01
404=\u9875\u9762\u6CA1\u6709\u627E\u5230\uFF01
500=\u5185\u90E8\u670D\u52A1\u5668\u9519\u8BEF\uFF01
error=\u670D\u52A1\u5668\u53D1\u751F\u9519\u8BEF\uFF01
\ No newline at end of file
#off/fatal/error/warn/info/debug/all
log4j.debug=false
log4j.rootLogger=info,stdout,log,errorlog
# Console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
### Log ###
log4j.appender.log = org.apache.log4j.DailyRollingFileAppender
log4j.appender.log.File = ../logs/zheng-notify-server.log
log4j.appender.log.Append = true
log4j.appender.log.Threshold = DEBUG
log4j.appender.log.DatePattern='.'yyyy-MM-dd
log4j.appender.log.layout = org.apache.log4j.PatternLayout
log4j.appender.log.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %p ] [ %C{1}.java :%L(%M)] %m%n
### Error ###
log4j.appender.errorlog = org.apache.log4j.DailyRollingFileAppender
log4j.appender.errorlog.File = ../logs/zheng-notify-server.error.log
log4j.appender.errorlog.Append = true
log4j.appender.errorlog.Threshold = ERROR
log4j.appender.errorlog.DatePattern='.'yyyy-MM-dd
log4j.appender.errorlog.layout = org.apache.log4j.PatternLayout
log4j.appender.errorlog.layout.ConversionPattern =%-d{yyyy-MM-dd HH:mm:ss} [ %p ] [ %C{1}.java :%L(%M)] %m%n
#Spring logging configuration
log4j.category.org.springframework = warn
\ No newline at end of file
app.name=zheng-notify
profile.env=dev
zheng.ui.path=http://ui.zhangshuzheng.cn:1000/
\ No newline at end of file
app.name=zheng-notify
profile.env=prod
zheng.ui.path=http://ui.zhangshuzheng.cn:1000/
\ No newline at end of file
app.name=zheng-notify
profile.env=test
zheng.ui.path=http://ui.zhangshuzheng.cn:1000/
\ No newline at end of file
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:property-placeholder location="classpath:config.properties"/>
<!-- thymeleaf视图 -->
<bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="order" value="0"/>
<property name="prefix" value="${zheng.ui.path}"/>
<property name="suffix" value=""/>
<property name="templateMode" value="HTML"/>
<property name="cacheable" value="false"/>
<property name="characterEncoding" value="UTF-8"/>
<!--<property name="cacheable" value="true"/>-->
<!--<property name="cacheTTLMs" value="10000"/>-->
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver"/>
<property name="enableSpringELCompiler" value="true"/>
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="viewNames" value="*.html"/>
</bean>
<!-- Jsp视图 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="1"/>
<property name="viewNames" value="*.jsp"/>
<property name="prefix" value="/WEB-INF/jsp"/>
<property name="suffix" value=""/>
<property name="contentType" value="text/html; charset=utf-8"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>
</beans>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- 强制进行转码 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<!-- 默认的spring配置文件是在WEB-INF下的applicationContext.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext*.xml
</param-value>
</context-param>
<!-- 日志配置文件 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<!-- springMVC的核心控制器 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:springMVC-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- session配置 -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<!-- 欢迎页面 -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册