我使用以下命令启动netty jax rs服务器:
NettyJaxrsServer netty = new NettyJaxrsServer();
netty.setHostname(HOST);
netty.setPort(port);
netty.setDeployment(resteasyDeployment);
// Some optional extra configuration
netty.setKeepAlive(true);
netty.setRootResourcePath("/");
netty.setSecurityDomain(null);
netty.setIoWorkerCount(16);
netty.setExecutorThreadCount(16);
LOGGER.info("Starting REST server on " + System.getenv("HOSTNAME"));
// Start the server
//("Starting REST server on " + System.getenv("HOSTNAME"));
netty.start();
LOGGER.info("Started!");
这很好但是没有办法检查服务器是否实际启动并且可以接受REST请求。我一直在使用REST接口的try / catch请求,如果我得到异常,则在catch块中等待,然后重试。它有效,但有点乱:
private void bringUpInterface(SlaveRestInterface slaveAgent, Target localTarget) {
LOGGER.info("Bringing up interface on " + localTarget.getHostName());
Response r = null;
long startTime = System.currentTimeMillis();
boolean isReady = false;
long wait = 50;
int count = 0;
try {
while (!isReady) {
try {
r = slaveAgent.ping();
r.close();
isReady = true;
long endTime = System.currentTimeMillis();
SYS_LOGGER.info(" [OK]" + " (" + (endTime - startTime + "ms") + ")");
} catch (ProcessingException ce) {
if(r != null) {
r.close();
}
if (count == DEFAULT_TIMEOUT_SEC) {
throw new TimeoutException("Failed to start REST interface in " + DEFAULT_TIMEOUT_SEC
+ "second(s)");
}
Thread.sleep(wait);
wait *= 2;
count++;
} catch (Exception e) {
isReady = false;
throw new FatalException("Couldn't connect to REST interface on " + localTarget.getHostName());
}
}
} catch (InterruptedException | TimeoutException ie) {
isReady = false;
throw new FatalException("REST interface did not come up on " + localTarget.getHostName());
}
}
我想知道我是否可以覆盖start()方法,并添加代码来ping端点,可能使用HttpRequest。似乎没有任何编程方式来查看服务器是否已启动。
答案 0 :(得分:0)
如果你可以加入Netty,请带上你的 #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
enum class EventType {ARRIVE, LEAVE};
struct Event{
int time;
EventType type;
Event(){}
Event(int tm, EventType t) : time(tm), type (t){}
// inline bool operator<(const Event& e) const {return time < e.time || (time == e.time && type==EventType::LEAVE);}
};
bool eventCompare(const Event& e1, const Event& e2) {
if(e1.time < e2.time){
return true;
} else if(e1.time == e2.time) {
if(e1.type == EventType::LEAVE && e2.type == EventType::ARRIVE) {
return true;
} else {
return false;
}
} else {
return false;
}
}
int main()
{
int visits;
int timeStart, timeEnd;
int maxVisits, maxDuration, localMaxVisits, localStartTime, localEndTime, duration;
std::vector<Event> events;
std::vector<Event>::const_iterator it;
// Get each Case from stdin.
// 0 is the special stopping case.
while(cin>>visits && visits!=0) {
events.clear();
// Read all the visits, for each one save two entries to the visits-vector,
// one for the arrival time another for the leaving time.
for(int i=1; i<=visits; ++i){
cin>>timeStart>>timeEnd;
Event eStart(timeStart, EventType::ARRIVE);
Event eEnd(timeEnd, EventType::LEAVE);
events.push_back(eStart);
events.push_back(eEnd);
}
// Sorting the vector so that events are ordered by their arrival time.
// In case two events coincide on arrival time, we place leaving events before arrival events.
std::sort(events.begin(), events.end(), eventCompare);
maxVisits=0;
maxDuration=0;
localMaxVisits=0;
localStartTime=0;
localEndTime=0;
// Loop through the sorted vector
for(it=events.begin(); it!=events.end(); ++it){
// If the event type is arrival
if((*it).type == EventType::ARRIVE) {
// We only reset the localStartTime if there is no
// gap between the last endtime and the current start time
// For example two events 11-13 and 13-15 should be treated as one long event
// with duration 11-15
if(localEndTime < (*it).time) {
localStartTime = (*it).time;
}
localMaxVisits++;
} else { // Event type leave
// Calculate the duration
duration = (*it).time - localStartTime;
// If we have a new max in overlaps or equal overlaps but larger duration than previous
// We save as the global max.
if(localMaxVisits > maxVisits || (localMaxVisits == maxVisits && duration>maxDuration)) {
maxVisits=localMaxVisits;
maxDuration = duration;
}
localMaxVisits--;
localEndTime= (*it).time;
}
}
cout<<maxVisits<<" "<<maxDuration<<endl;
}
return 0;
}
并执行以下操作:
ServerBootstrap