作者 王勇

收消息,基本完善

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectInspectionProfilesVisibleTreeState">
<entry key="Project Default">
<profile-state>
<expanded-state>
<State />
<State>
<id>Java</id>
</State>
<State>
<id>Serialization issuesJava</id>
</State>
</expanded-state>
<selected-state>
<State>
<id>SerializableHasSerialVersionUIDField</id>
</State>
</selected-state>
</profile-state>
</entry>
</component>
</project>
\ No newline at end of file
... ...
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/
... ...
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="message_bus_sdk" />
</profile>
</annotationProcessing>
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="message_bus_sdk" options="-parameters" />
</option>
</component>
</project>
\ No newline at end of file
... ...
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" defaultCharsetForPropertiesFiles="UTF-8">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="PROJECT" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
... ...
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="JavaDoc" enabled="true" level="WARNING" enabled_by_default="true">
<option name="TOP_LEVEL_CLASS_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="" />
</value>
</option>
<option name="INNER_CLASS_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="" />
</value>
</option>
<option name="METHOD_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="@return@param@throws or @exception" />
</value>
</option>
<option name="FIELD_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="" />
</value>
</option>
<option name="IGNORE_DEPRECATED" value="false" />
<option name="IGNORE_JAVADOC_PERIOD" value="true" />
<option name="IGNORE_DUPLICATED_THROWS" value="false" />
<option name="IGNORE_POINT_TO_ITSELF" value="false" />
<option name="myAdditionalJavadocTags" value="date" />
</inspection_tool>
</profile>
</component>
\ No newline at end of file
... ...
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="http://maven.aliyun.com/nexus/content/groups/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
... ...
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
... ...
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
... ...
URL_CONSUMER_ONE=http://192.168.1.33:9032/bus/rabbit/consumer_one
URL_CONSUMER_MORE=http://192.168.1.33:9032/bus/rabbit/consumer_more
\ No newline at end of file
... ...
... ... @@ -12,6 +12,13 @@
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
<properties>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
... ... @@ -20,6 +27,21 @@
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
... ... @@ -77,9 +99,6 @@
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
</dependencies>
<build>
... ...
... ... @@ -5,15 +5,20 @@ import com.sunyo.wlpt.message.bus.sdk.response.BusResult;
import java.io.IOException;
/**
* Hello world!
* @author 子诚
*/
public class App {
public static void main(String[] args)
public static void main(String[] args) throws IOException
{
MessageRequest request = new MessageRequest();
BusResult result = request.getMessageOne("zicheng", "S_zicheng",
BusResult resultOne = request.getMessageOne("zicheng", "S_zicheng",
"V_zicheng", "V_zicheng_zicheng_R");
System.out.println(result);
BusResult resultMore = request.getMessageMore("zicheng", "S_zicheng",
"V_zicheng", "V_zicheng_zicheng_R", 5);
System.out.println(resultOne);
System.out.println(resultMore);
}
}
... ...
... ... @@ -6,8 +6,11 @@ import com.sunyo.wlpt.message.bus.sdk.utils.OkHttpUtils;
import okhttp3.MediaType;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
/**
* @author 子诚
... ... @@ -16,35 +19,73 @@ import java.util.HashMap;
*/
public class BusMessage {
private static final String URL_CONSUMER_ONE = "http://192.168.1.33:9032/bus/rabbit/consumer_one";
private static final String URL_CONSUMER_MORE = "http://192.168.1.33:9032/bus/rabbit/consumer_more";
/**
* 指定队列,接收一条消息
*
* @param RUSR 接收者
* @param SERV 服务器名称
* @param VSHT 虚拟主机名称
* @param RCVR 队列名称
* @return
* @throws IOException
*/
public static ResultJson consumerOne(String RUSR, String SERV, String VSHT, String RCVR) throws IOException
{
HashMap<String, Object> params = new HashMap<>();
params.put("RUSR", RUSR);
params.put("SERV", SERV);
params.put("VSHT", VSHT);
params.put("RCVR", RCVR);
private static final MediaType MEDIA_TYPE_TEXT = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
OkHttpUtils okHttp = new OkHttpUtils();
String url_consumer_one = getUrl("URL_CONSUMER_ONE");
String json = okHttp.doGet(url_consumer_one, params);
ResultJson result = GsonUtil.GsonToBean(json, ResultJson.class);
return result;
}
/**
* 指定队列,消费一条消息
* 指定队列,接收多条消息
*
* @param RUSR 接收者
* @param SERV 服务器名称
* @param VSHT 虚拟主机名称
* @param RCVR 队列名称
* @param CUNT 接收消息的数量
* @return
* @throws IOException
*/
public static ResultJson consumerOne(String RUSR, String SERV, String VSHT, String RCVR)
public static ResultJson consumerMore(String RUSR, String SERV, String VSHT, String RCVR, Integer CUNT) throws IOException
{
HashMap<String, String> params = new HashMap<>();
HashMap<String, Object> params = new HashMap<>();
params.put("RUSR", RUSR);
params.put("SERV", SERV);
params.put("VSHT", VSHT);
params.put("RCVR", RCVR);
params.put("CUNT", CUNT);
OkHttpUtils okHttp = new OkHttpUtils();
String json = okHttp.doGet(URL_CONSUMER_ONE, params);
String url_consumer_more = getUrl("URL_CONSUMER_MORE");
String json = okHttp.doGet(url_consumer_more, params);
ResultJson result = GsonUtil.GsonToBean(json, ResultJson.class);
return result;
}
/**
* 根据url名称,动态获取url
*
* @param urlName url名称
* @return url信息
* @throws IOException
*/
public static String getUrl(String urlName) throws IOException
{
Properties properties = new Properties();
FileInputStream fis = new FileInputStream("config\\url.properties");
properties.load(fis);
fis.close();
String url = properties.getProperty(urlName);
return url;
}
}
... ...
... ... @@ -3,6 +3,9 @@ package com.sunyo.wlpt.message.bus.sdk.request;
import com.sunyo.wlpt.message.bus.sdk.response.BusResult;
import com.sunyo.wlpt.message.bus.sdk.response.ResultJson;
import java.io.IOException;
import java.util.List;
/**
* @author 子诚
* Description:
... ... @@ -21,7 +24,7 @@ public class MessageRequest {
* @param RCVR 接收队列名称
* @return
*/
public BusResult getMessageOne(String RUSR, String SERV, String VSHT, String RCVR)
public BusResult getMessageOne(String RUSR, String SERV, String VSHT, String RCVR) throws IOException
{
ResultJson resultJson = BusMessage.consumerOne(RUSR, SERV, VSHT, RCVR);
if (resultJson == null) {
... ... @@ -38,4 +41,23 @@ public class MessageRequest {
BusResult result = new BusResult(true, code, msg, data);
return result;
}
public BusResult getMessageMore(String RUSR, String SERV, String VSHT, String RCVR, Integer CUNT) throws IOException
{
ResultJson resultJson = BusMessage.consumerMore(RUSR, SERV, VSHT, RCVR, CUNT);
if (resultJson == null) {
}
String code = resultJson.getCode();
String msg = resultJson.getMsg();
if (!SUCCESS_ONE_CODE.equals(resultJson.getCode())) {
String resultMessage = "错误代码:" + code + ";错误信息:" + msg;
BusResult errorResult = new BusResult(false, code, resultMessage);
return errorResult;
}
List<String> dataList = (List<String>) resultJson.getData();
BusResult result = new BusResult(true, code, msg, dataList);
return result;
}
}
... ...
... ... @@ -108,4 +108,16 @@ public class BusResult {
{
this.dataList = dataList;
}
@Override
public String toString()
{
return "BusResult{" +
"isOk=" + isOk +
", code='" + code + '\'' +
", message='" + message + '\'' +
", data='" + data + '\'' +
", dataList=" + dataList +
'}';
}
}
... ...
package com.sunyo.wlpt.message.bus.sdk.utils;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import java.net.URLEncoder;
... ... @@ -15,8 +15,8 @@ import java.util.concurrent.TimeUnit;
* Description:
* 时间:2020/9/3 15:22
*/
@Slf4j
public class OkHttpUtils {
private static Logger log = Logger.getLogger(OkHttpUtils.class);
private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
private static final MediaType MEDIA_TYPE_TEXT = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
... ... @@ -57,7 +57,7 @@ public class OkHttpUtils {
return commonRequest(request);
}
public String doGet(String url, HashMap<String, String> params)
public String doGet(String url, HashMap<String, Object> params)
{
if (isBlankUrl(url)) {
return null;
... ... @@ -102,9 +102,9 @@ public class OkHttpUtils {
Response response = call.execute();
if (response.isSuccessful()) {
re = response.body().string();
log.info("request url:{};response:{}", request.url().toString(), re);
// log.info("request url:{};response:{}", request.url().toString(), re);
} else {
log.info("request failure url:{};message:{}", request.url().toString(), response.message());
// log.info("request failure url:{};message:{}", request.url().toString(), response.message());
}
} catch (Exception e) {
log.error("request execute failure", e);
... ... @@ -142,7 +142,7 @@ public class OkHttpUtils {
return request;
}
private Request getRequestForGet(String url, HashMap<String, String> params)
private Request getRequestForGet(String url, HashMap<String, Object> params)
{
Request request = new Request.Builder()
.url(getUrlStringForGet(url, params))
... ... @@ -158,15 +158,20 @@ public class OkHttpUtils {
return request;
}
private String getUrlStringForGet(String url, HashMap<String, String> params)
private String getUrlStringForGet(String url, HashMap<String, Object> params)
{
StringBuilder urlBuilder = new StringBuilder();
urlBuilder.append(url);
urlBuilder.append("?");
if (params != null && params.size() > 0) {
for (Map.Entry<String, String> entry : params.entrySet()) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
try {
urlBuilder.append("&").append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "UTF-8"));
urlBuilder.append("&").append(entry.getKey()).append("=").append(
"CUNT".equals(entry.getKey())
? entry.getValue()
: URLEncoder.encode((String) entry.getValue(), "UTF-8")
);
} catch (Exception e) {
urlBuilder.append("&").append(entry.getKey()).append("=").append(entry.getValue());
}
... ...