Set up a connection that times out for Apache Geode Client - geode

I have a windows desktop application on .NET Framework that a user can interact with. There is a "connect" button that sets up Apache Geode client connection to Geode Server.
When Geode Server is down (Locator - there is only 1) the desktop application hangs indefinitely and needs to be forcefully closed.
This is how we connenect to the server and hangs on last line indefinitely:
PoolFactory poolFactory = _cache.GetPoolManager()
.CreateFactory()
.SetSubscriptionEnabled(true)
.AddLocator(_host, _port);
return poolFactory.Create(_serviceName);
I would like to add a time-out period for this method to return or throw exceptions or anything.
Alternatively, I've wrapped it on a different kind of timer outside of this to just return, but when trying to run the above code again (to try to connect the application again) the pool is still trying to connect.
I have tried to destroy it like this:
//destroy the pool
var poolManager = _cache.GetPoolManager();
if (poolManager != null)
{
var pool = poolManager.Find(_serviceName);
if (pool != null && pool.Destroyed == false)
pool.Destroy();
}
But then the pool.Destroy(); hangs indefinateley
How can I have a mechanism to attempt to connect to the Geode server, return if not connected within 10 seconds, then leave and are able to try to connect again using the same cache?
Note above was trying to re-use the same cache, probably setting cache to null and trying all over again would work, but I am looking for a more correct way to do it.

Depending on what version of .NET native client you are using. There is a property in 10.x connect-timeout that should work. Additionally, you can wrap your connection code under try{...}catch{} block and handle an exception for Apache.Geode.Client.TimeoutException and/or ": No locators available".
Example:
var _cache = new CacheFactory()
.Set("log-level", "config")
.Set("log-file", "C:\temp\test.log")
.Set("connect-timeout", "26000ms")
.Create();
_cache.GetPoolManager()
.CreateFactory()
.SetSubscriptionEnabled(true)
.AddLocator(hostname-or-ip", port-number)
.Create("TestPool");

Related

MQTT connection creation and subscribe

I'm setting up a new mqtt conection in my app but there is a problem when i would like to create the main connection of mqtt.
I'm using mqtt.js.
I've tried all what is done in MQTT documentation but nothing happens..
mqttFunction(){
var mqtt = require('mqtt');
var client = mqtt.connect([{host: 'localhost', port: '1883'},]);
client.subscribe('presence')
client.on('message', function (topic, message) {
console.log(message);
});
}
I expect the output of the mqtt broker to be 'ON' when i asked it to respond.
The error is: ERROR ReferenceError: process is not defined
The documentation you followed is intended for Node.js and various other back-end JavaScript frameworks. Even though it uses NPM, Ionic ultimately produces a front-end framework, and its applications run a bit differently.
For example, Ionic programs may not have a global process variable like Node.js. mqtt.js expects this variable, with code like:
if (commist.parse(process.argv.slice(2)) !== null){...}
You could declare a process object, and get past this particular error. Other obstacles could come up.
var process = {env : {NODE_ENV: 'production'}}
If there are still issues with that, you could try the instructions for browser usage, which point to a specially compiled version, like https://unpkg.com/mqtt#3.0.0/dist/mqtt.min.js. I have had less luck with mqtt.js in the browser, and you may want an alternative like web-mqtt-cient / Paho if more complex connections are involved.

Java ME Connector.open() is returning null

I'm currently developing a project for school using Java ME and Intellij. I need to create both a client and server using the javax.microedition.io library. However, I tried running some sample code for creating both a server and a client and I get a return value of null for both of these lines:
ServerSocketConnection server = (ServerSocketConnection) Connector.open("socket://:" + port);
SocketConnection client = (SocketConnection) Connector.open("socket://" + hostname + ":" + port);
Is there something I'm missing? Intellij has networking permissions and I'm simply trying to test this on my local host. I haven't been able to find any documentation to indicate why these functions would return null. I've tried other protocols as well (including bogus ones) and no exceptions are being thrown at all.
EDIT
Turns out all the classes I have found under javax.microedition.io are unimplmented (i.e. simply return null or similar values such as 0) I will make a new post regarding this problem.

C# MVC5 classic ADO.NET when to open connection

I'm using MVC5 with classic ADO.NET objects such as sqldatareader and sqldataadapter and sqlconnection and so on....
My controllers are creating a connection while initializing because I need to send the request object to the class holding the sqlconnection for something irrelevant to the question so my controller has an override void
protected override void Initialize(RequestContext requestContext)
{
base.Initialize(requestContext);
db = new db(Request);
db.Connect();
}
Where db is my class and the method (connect) will create the sqlconnection object and open a connection...
and to close the connection I used the controller's dispose method as follows
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (db != null)
{
db.Close();
db = null;
}
}
and everything works fine then at one moment I got a weird server error (can't connect to db) please notice that my host is smarterasp.net
I can connect to database remotely using my home computer and I can connect to the web host as well so the problem is between my webhost and my database host, or between my application and my database host...
or it could be something related to the connection pooling even though the server error doesn't give me any details or stack trace(hens error is not inside my app thread)....
and I've fixed the problem by opening (remote iis) tab of smarterasp.net's control panel and clicked on (fix ACL) which I have no idea what it does but it fixed my problem.... temporarily :( unfortunately the problem reoccurred many times after that
so my question is in short format
is it good practice to open the connection while I'm initializing the controller and close it while the controller disposing???
and what do you think the error reason is??
finally I want to apologize if the question wasn't clear enough because English is not my first language (obviously)....
thanks a lot
so my question is in short format is it good practice to open the
connection while I'm initializing the controller and close it while
the controller disposing???
I do not think that is a good approach. You shouldn't open / close database connections and / or access the database from your controller. The controller should be as "thin" as possible. Additionally - the connection should be kept open for as short a period of time as possible and let ADO.NET connection pooling handle the details for you.
I also recommend wrapping your connection in a using block as it will implicitly call the close method:
using (SqlConnection connection = new SqlConnection(
connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
command.Connection.Open();
command.ExecuteNonQuery();
}
You did not say what the exact error is. At first glance, you aren't even checking to see if the connection is open before you try closing it. You should check the connection state before you try to explicitly close it and this should happen outside of the controller. Though I recommend that you wrap your SqlConnection in a using block (mentioned above).
EDIT
I read your comment. You are trying to manage the connection within the context of the controller's lifecycle and I suspect this is your issue.
If you were using Entity Framework (or possibly an another ORM), an IoC with "per-request lifestyle" - then the IoC container would properly dispose your context (connection) at the end of each request, and serve a new instance at each new one.
Perhaps this an option you can explore if you want to manage your database connection this way.

Zuul connection pool size

I have a question about configuring the size of the connection pool when using Zuul by itself, and not using Ribbon or other Netflix components.
We have a system that uses Zuul to proxy requests to a Mule server. We are only using Zuul and not Ribbon. We have defined 4 routes that call the Mule services. One of these services is long running, probably around 3 seconds per call.
When we load the system with 40 simultaneous users we get this error
org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool
at org.apache.http.impl.conn.tsccm.ConnPoolByRoute.getEntryBlocking(ConnPoolByRoute.java:412)
at org.apache.http.impl.conn.tsccm.ConnPoolByRoute$1.getPoolEntry(ConnPoolByRoute.java:298)
at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager$1.getConnection(ThreadSafeClientConnManager.java:238)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:423)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:115)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
at org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter.forwardRequest(SimpleHostRoutingFilter.java:262)
at org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter.forward(SimpleHostRoutingFilter.java:225)
at org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter.run(SimpleHostRoutingFilter.java:177)
at com.netflix.zuul.ZuulFilter.runFilter(ZuulFilter.java:112)
When I looked through the code to figure out how to change the size of the connection pool and found this code
private static ClientConnectionManager newConnectionManager() throws Exception {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(registry);
cm.setMaxTotal(Integer.parseInt(System.getProperty("zuul.max.host.connections", "200")));
cm.setDefaultMaxPerRoute(Integer.parseInt(System.getProperty("zuul.max.host.connections", "20")));
return cm;
}
At first I thought all I have to do is increase the value for zuul.max.host.connections and that would increase the size of per route max, but then I noticed that the same system property is used to set the total max number of connections.
Is setting the value of this system parameter the correct way to control the pool sizes? Or should we be using another component such as Ribbon to better manage these connections?
you can config
zuul.host.maxTotalConnections=1000
zuul.host.maxPerRouteConnections=100
If you still looking for solution, i took the following approach.
1- Disable ( SimpleHostRoutingFilter ) by passing
( -Dzuul.SimpleHostRoutingFilter.route.disable=true ) as system property.
2- Write your own customized Routing filter, in my case i copied ( SimpleHostRoutingFilter ), and make some modification to be able to set these properties .

transaction problem with entity framework 4

I'm trying to implement a transaction with entity framework 4. From what I've read, the code below is correct. The SaveChanges works fine but as soon as I hit the first ExecuteFunction call I get the following exception:
The underlying provider failed on
Open. --->
System.Transactions.TransactionManagerCommunicationException:
Network access for Distributed
Transaction Manager (MSDTC) has been
disabled. Please enable DTC for
network access in the security
configuration for MSDTC using the
Component Services Administrative
tool.
I've logged on to the database server and I don't see a service called Distributed Transaction Manager but I do see Distributed Transaction Coordinator and it is started. I'm not sure what I need to change to allow this to work. Anyone know? Thanks.
Here's the code.
using (var h = new WhaleEntities(ConnectionHelper.DBConnectString))
{
using (TransactionScope ts = new TransactionScope())
{
h.Sites.AddObject(s);
h.SaveChanges(SaveOptions.DetectChangesBeforeSave);
retval = s.SiteID;
h.ExecuteFunction("UpdateSiteInterfaceList", new ObjectParameter("pSiteID", retval), new ObjectParameter("pList", "10"));
h.ExecuteFunction("UpdateSiteInterfaceRequiredList", new ObjectParameter("pSiteID", retval),new ObjectParameter("pList", "Email"));
h.ExecuteFunction("UpdateSiteInterfaceAlwaysShownList", new ObjectParameter("pSiteID", retval),new ObjectParameter("pList", "10"));
h.ExecuteFunction("UpdateSiteInterfaceAlwaysRequiredList",new ObjectParameter("pSiteID", retval),new ObjectParameter("pList", "Email"));
ts.Complete();
//changes must be accepted manually once transaction succeeds.
h.AcceptAllChanges();
}
}
See here: How do I enable MSDTC on SQL Server?