When client create an instant on SenderThread. It can send data to and from.
But when client goes for suspend mode and come back from suspend. The last created threads get exception on
resume. and no data is sent.
Exception Details received :
displayText = Exception
message =
name = Exception
className = N4Poco9ExceptionE
Here is the code:
class SenderThread: public Poco::Runnable
{
public:
MyThread(const std::string& msg):
Msg(msg);
{
}
void run()
{
try {
SendData(msg);
} catch(Exception exp) {
std::cout<<"displayText = "<<e.displayText()<<std::endl;
std::cout<<"message = "<<e.message()<<std::endl;
std::cout<<"name = "<<e.name()<<std::endl;
std::cout<<"className = "<<e.className()<<std::endl;
}
}
private:
std::string Msg;
};
How can I get more details on the exception and how to handle this exception?.
Edited After Günter Obiltschnig comment:
I am able to catch the proper exception.
displayText = Invalid argument
name = Invalid argument
Some time i see socket closed exception. When system goes to suspend mode then all the socket is closed by system(os). Now upon resume application tries to open the socket again it throw error in the Poco::Net::Socket::Socket(Poco::Net::Socket const&). any help on this please
You have to catch the exception by (const) reference in order to get useful information out of it.
catch (const Poco::Exception& exc)
{
std::cerr << exc.displayText() << std::endl;
}
Related
I want to receive updates from websockets defined in the coinbase pro API.
For example I'd like to register the heartbeat channel, that should be received once per second according to documentation.
I've tried to register to the web socket, but I obtain only the subscriptions message in reply:
{"type":"subscriptions","channels":[{"name":"heartbeat","product_ids":["ETH-EUR"]}]}
After that I'm not receiving anything anymore. What should I do in order to listen to the messages of the client
This is the piece of code involved (modified a bit, it's in a class, m_webSocket is a class member that should be used also elsewhere, and it should be closed in the destructor):
void EndpointClient::connectToWebSockets() {
constexpr auto WebSocketFeed{ U("wss://ws-feed.pro.coinbase.com") };
constexpr auto RequestStatus{ "{\"type\": \"subscribe\", \"channels\" : [{ \"name\": \"heartbeat\", \"product_ids\" : [\"ETH-EUR\"] }]}" };
web::websockets::client::websocket_client m_webSocket;
try {
m_webSocket.connect(WebSocketFeed).wait();
auto receivedTast = m_webSocket.receive().then([this](websocket_incoming_message ret_msg) {
auto s = ret_msg.extract_string();
m_logger->info("s = " + s.get());
});
websocket_outgoing_message msg;
msg.set_utf8_message(RequestStatus);
m_webSocket.send(msg).wait();
receivedTast.wait();
m_webSocket.close().wait();
}
catch (websocket_exception& e) {
m_logger->error("coinbase: exception when setting websocket: " + string{ e.what() });
}
catch (std::exception& e) {
m_logger->error("coinbase: exception when setting websocket: " + string{ e.what() });
}
}
The .receive() method only gets one message. If you want to receive more incoming messages, you need to run it in a loop. Or you can use a websocket_callback_client instead so that it can listen automatically (websocket_callback_client.set_message_handler() method).
I am trying to write a long running Subscriber service in Java. I have set up the Listeners to listen to any failures inside the Subscriber service. I am trying to make this fault tolerant and I do not quite understand few things, Below are my doubts/questions.
I have followed the basic setup shown here https://github.com/googleapis/google-cloud-java/blob/master/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberSnippets.java. Specifically, I have setup addListener as shown below.
As shown in the following code, initializeSubscriber acts a state variable which will determine if the Subscriber service should restart. Inside the while loop, this variable is continuously monitored to determine if the restart is required.
My question here is,
1. How do I raise an exception inside Subscriber.Listener's failed method and capture it in the main while loop. I tried throwing a new Exception() in failed method and catching it in catch block inside while, However, I am unable to compile the code as it is a checked exception.
2. As shown here, I use Java Executor thread to run the Listener. How do I handle the Listener failures ? Will I able to catch Listener failures under general Exception catch block as shown here ?
try {
boolean initializeSubscriber = true;
while (true) {
try {
if (initializeSubscriber) {
createSingleThreadedSubscriber();
addErrorListenerToSubscriber();
subscriber.startAsync().awaitRunning();
initializeSubscriber = false;
}
// Checks the status of subscriber service every minute
Thread.sleep(60000);
} catch (Exception ex) {
LOGGER.error("Could not start the Subscriber service", ex);
cleanupSubscriber();
initializeSubscriber = true;
}
}
} catch (RuntimeException e) {
} finally {
shutdown();
}
private void addErrorListenerToSubscriber() {
subscriber.addListener(
new Subscriber.Listener() {
#Override
public void failed(Subscriber.State from, Throwable failure) throws RuntimeException {
LOGGER.info("Subscriber reached a failed state due to " + failure.getMessage()
+ ",Restarting Subscriber service");
initializeSubscriber = true;
}
},
Executors.newSingleThreadExecutor());
}
private void cleanupSubscriber() {
try {
if (subscriber != null) {
subscriber.stopAsync().awaitTerminated();
}
if (!subscriptionListener.isShutdown()) {
subscriptionListener.shutdown();
}
} catch (Exception ex) {
LOGGER.error("Error in cleaning up Subscriber thread " + ex);
}
}
It should not be necessary to add a listener to the subscriber if you just want to recreate the subscriber on a failure. You could instead catch the exception on awaitTerminated:
try {
boolean initializeSubscriber = true;
while (initializeSubscriber) {
try {
createSingleThreadedSubscriber();
subscriber.startAsync().awaitRunning();
initializeSubscriber = false;
subscriber.awaitTerminated();
} catch (Exception ex) {
LOGGER.error("Error in the Subscriber service", ex);
cleanupSubscriber();
initializeSubscriber = true;
}
}
} catch (RuntimeException e) {
} finally {
shutdown();
}
If the subscriber shutdown successfully because of a call to stopAsync, then awaitTerminated will not throw an exception. If there was some kind of exception, then awaitTerminated will throw an IllegalStateException because the state will be FAILED instead of TERMINATED.
Note that transient errors are handled by the library itself. For example, if the server become briefly unavailable due to a network hiccup, the library will seamlessly reconnect and continue to deliver messages. Failures that result in a change in state for the subscriber are likely permanent failures such as permission issues (where the account running the subscriber does not have permission to subscribe to the subscription) or resource issues (such as the subscription having been deleted). In these permanent failure cases, recreating the subscriber will likely just result in the same error unless one takes manual steps to intervene and fix the problem.
I instantiate a PostgreSQL connection through libpqxx. I query the database and get correct response. After that I tried the following error case: after instance of pqxx::connection has been created, I pause my program, manually kill the Postgre's connection process from a Linux's command shell and resume the program. It continues until it tries to create new transaction pqxx::work where it throws pqxx::broken_connection. I handle this exception and try to reconnect with a call to pqxx::connection::activate() but another pqxx::broken_connection gets thrown. How to reconnect to DB without instantiate another pqxx::connection?
P.S. reactivation is not inhibited. I use the standard connection type -
namespace pqxx
{
typedef basic_connection<connect_direct> connection;
}
Ok, nobody has answered. I noticed that after I manually kill the process behind the connection after several successive calls to pqxx::connection::activate, it gets reconnected, so that's my workaround.
class dbconnection : public pqxx::connection
{
public:
dbconnection(std::string options) : pqxx::connection(options) { };
void reconnect()
{
static int times = 0;
try
{
times++;
if(!this->is_open())
{
this->activate();
}
times = 0;
}
catch(const pqxx::broken_connection & e)
{
if(times > 10)
{
times = 0;
return;
}
this->reconnect();
}
};
};
I call dbconnection::reconnect each time after I catch a pqxx::broken_connection. Let me know do you have better solution?
I am using the Casablanca REST SDK to make a webservice. The webservice has the client pass JSON through the URL and processes it. However in some cases if I send JSON that contains an array, it will crash the service. The strange part is the handler method that actually handles the JSON runs all the way through and ends before the error is given. My main function is:
int main()
{
std::string ip;
std::string port;
std::ifstream inputStream;
inputStream.open("ip_port.config");
while(!inputStream.eof())
{
inputStream >> ip;
inputStream >> port;
}
inputStream.close();
std::string uri = "http://"+ip+":"+port+"/";
//set up URL
web::http::uri u(U(uri));
//set up listener for URL
web::http::experimental::listener::http_listener listener(u);
listener.support(methods::GET, handle_get);
try
{
pplx::task<void> l = listener.open();
while(true)
{
l.wait();
sleep(1);
}
listener.close().wait();
}
catch(int e)
{
std::stringstream errorMessage;
errorMessage << "Error: " << e;
logRequest(errorMessage.str(), "Error Message");
}
return 0;
}
I have put two prints: "Beginning of Handler" and "End of Handler" in the handle_get function, which are both printed, leading me to beleive that the cause of the error has something to do with the while loop with the listener, but the error it gives has nothing to do with the listener:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr
Im trying to wrap my head around what about the listener could return that error.
I already do know, that it is impossible to simply detect if socket is disconnected or not - the server and clients must shout "Can you hear me?" and "Yeah I can." just like we do on skype.
But when boost::asio socket is disconnected from other side I obtain Unhanded exception when trying to read from socket. This is kind of disconnect detection useful enough for me. Can I handle that exception, so instead of crashing, the program will produce message in the console?
Some code for those who need it for everything:
bool SocketClient::read(int bytes, char *text) {
char buffer = 0;
int length = 0;
while(bytes>0) {
size_t len = sock.receive(boost::asio::buffer(&buffer, 1)); //boom: UNHANDLED EXCEPTION
bytes--;
text[length] = buffer;
length++;
}
return true;
}
Because I am connecting to minecraft server, I know when the client is disconnected - exception is caused on any read/write attempt.
try
{
size_t len = sock.receive(boost::asio::buffer(&buffer, 1)); //boom: UNHANDLED EXCEPTION
// More code ...
}
catch (const boost::system::system_error& ex)
{
if ( ex.code() == boost::asio::error::eof )
{
// Work your magic (console logging, retry , bailout etc.)
}
}
Please also take a look at the doc. In the worst case , you could infer the exception type from the debugger :)