1、solr的搜索客户端后台代码(Search_Client.java)package solrj;
import java.util.UUID;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.QueueingConsumer.Delivery;
import com.rabbitmq.client.AMQP.BasicProperties;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;
public class Search_Client
{
private Connection connection;
private Channel channel;
private String replyQueueName;
private QueueingConsumer consumer;
//在构造函数中,完成连接、信道、队列的初始化
public Search_Client() throws Exception
{
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("172.16.206.17");
connection = factory.newConnection();
//建立信道channel
channel = connection.createChannel();
//声明一个返回队列,名称由RabbitMQ随机生成
//replyQueueName=channel.queueDeclare().getQueue();
//声明一个返回队列,名称指定
replyQueueName="Solr_result";
channel.queueDeclare(replyQueueName, false, false, false, null);
consumer = new QueueingConsumer(channel);
channel.basicConsume(replyQueueName, true, consumer);
}
public String call(String Exchange,String message) throws Exception
{
String response = null;
String corrId = UUID.randomUUID().toString();
//请求信息属性:correlationId,callback队列名称
BasicProperties props = new BasicProperties
.Builder()
.correlationId(corrId)
.replyTo(replyQueueName)
.build();
//发送请求
channel.basicPublish(Exchange,"search", props, message.getBytes("UTF-8"));
System.out.println("发送搜索请求,等待中。。。");
//等待并解析返回结果
while (true)
{
Delivery delivery = consumer.nextDelivery();
response = new String(delivery.getBody(), "UTF-8");
break;
}
return response;
}
public void close() throws Exception
{
connection.close();
}
//将请求封装为JSON数据类型,key:搜索关键字
public static String createRequest(String search_key,int page)throws JSONException
{
JSONStringer msg = new JSONStringer();
//JSON数据定义:(client_name:1.0,time:epoch)
return msg
.object()
.key("key")
.value(search_key)
.key("page")
.value(page)
.endObject().toString();
}
public String search (String search_key)
{
String response = null;
Search_Client client = null;
String exchange="MsgExchange";
int page = 1;
try {
client = new Search_Client();
response = this.call(exchange,Search_Client.createRequest(search_key,page));
System.out.println("搜索结果\n " + response);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (this != null) {
try {
this.close();
}
catch (Exception ignore) {}
}
}
}
return response;
}
}
package solrj;
import java.util.UUID;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.QueueingConsumer.Delivery;
import com.rabbitmq.client.AMQP.BasicProperties;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;
public class Search_Client
{
private Connection connection;
private Channel channel;
private String replyQueueName;
private QueueingConsumer consumer;
//在构造函数中,完成连接、信道、队列的初始化
public Search_Client() throws Exception
{
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("172.16.206.17");
connection = factory.newConnection();
//建立信道channel
channel = connection.createChannel();
//声明一个返回队列,名称由RabbitMQ随机生成
//replyQueueName=channel.queueDeclare().getQueue();
//声明一个返回队列,名称指定
replyQueueName="Solr_result";
channel.queueDeclare(replyQueueName, false, false, false, null);
consumer = new QueueingConsumer(channel);
channel.basicConsume(replyQueueName, true, consumer);
}
public String call(String Exchange,String message) throws Exception
{
String response = null;
String corrId = UUID.randomUUID().toString();
//请求信息属性:correlationId,callback队列名称
BasicProperties props = new BasicProperties
.Builder()
.correlationId(corrId)
.replyTo(replyQueueName)
.build();
//发送请求
channel.basicPublish(Exchange,"search", props, message.getBytes("UTF-8"));
System.out.println("发送搜索请求,等待中。。。");
//等待并解析返回结果
while (true)
{
Delivery delivery = consumer.nextDelivery();
response = new String(delivery.getBody(), "UTF-8");
break;
}
return response;
}
public void close() throws Exception
{
connection.close();
}
//将请求封装为JSON数据类型,key:搜索关键字
public static String createRequest(String search_key,int page)throws JSONException
{
JSONStringer msg = new JSONStringer();
//JSON数据定义:(client_name:1.0,time:epoch)
return msg
.object()
.key("key")
.value(search_key)
.key("page")
.value(page)
.endObject().toString();
}
public String search (String search_key)
{
String response = null;
Search_Client client = null;
String exchange="MsgExchange";
int page = 1;
try {
client = new Search_Client();
response = this.call(exchange,Search_Client.createRequest(search_key,page));
System.out.println("搜索结果\n " + response);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (this != null) {
try {
this.close();
}
catch (Exception ignore) {}
}
}
}
return response;
}
}
简单说明:1、后台代码单独测试时,是包括main函数的,后面为了便于前台调用,将main函数改成了search函数。
2、构造函数中包括rabbit队列信道的建立,其他函数不必知道
2 solr客户端前台代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@ page import="com.lihai.solar.Search_Client"%>
<%! Search_Client search_Client = new Search_Client();
public String search(String key)
{
return search_Client.search(key);
}
%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
String result = " ";
request.setCharacterEncoding("utf-8");
String searchKey = request.getParameter("search_key") ;
if(null != searchKey){
System.out.println("search: " + searchKey);
result = search(searchKey);
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action = "index.jsp" method = "post">
<div align="center">
<input type = "text" name = "search_key">
<input type = "submit" value = "search">
</div>
<div>
<label>result:</label><br/>
<label><%=result %></label>
</div>
</form>
<% result = " " ;%>
</body>
</html>
简单说明:1、在前台代码中申请了类Search_Client的一个对象。当参数值不为空时,调用其search函数
3、导致问题及解决思路
问题
:前台代码中,首先,申请了类Search_Client的一个对象,申请对象的同时建立了一个信道;但是,只有当参数值不为空时,调用其search函数(search中有关闭信道的代码),当参数值不为空时,就不调用search函数,那么信道就一直被占用,堵塞。
解决方案:
在类中main函数中直接调用类的构造函数等涉及变量操作、资源操作的函数,这种方式本身就具有不合理性。所以需要将main函数和涉及资源操作的函数分开,即将main函数单独用一个类(如Search.java分离出来。
虽然现在很多java实例都这样(如下面代码), 但是不建议将main函数和涉及资源(包括变量、队列)放在同一个类中,这样会导致上述问题
public class ImpliClassInMain {
private String str = null;
public String getStr()
{
return str;
}
public void setStr(String str)
{
this.str = str;
}
public void sayStr()
{
System.out.println(getStr());
}
public static void main(String args[])
{
ImpliClassInMain impliClassInMain = new ImpliClassInMain();
impliClassInMain.setStr("Hello lihai!!!");
impliClassInMain.sayStr();
}
}