I'm setting up a dhtmlx grid for loading data using grid.render_table method in java, no rendering is done, I'm new in this
I've tried to implement the PHP 'contact_manager' demo that comes with dhtmlx in java, but grid.render_table does not populate the grid
try (PrintWriter out = response.getWriter())
{
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("jdbc/PsFlowWeb");
conn = ds.getConnection(username, password);
GridConnector gc = new GridConnector(conn);
gc.enable_log("temp.log", false);
LogManager.getInstance().log("any text here");
gc.render_table("TCiudades","csCodigoCiudad","csCodigoCiudad,csDescripcionCiudad,csTipoRiesgo");
}
catch (Throwable e) { e.printStackTrace(); }
I expect the grid be populated with data from server obtained by using grid.render_table method
Could you please, check your log file and the response coming from your data request.
Related
I have a java application running on WebSphere 8.5.5.12 server. I connect to other applications via MQ. I have faced a performance issue with the application and found that whenever the MQ reply is getting timeout, the Queue connection was not closed properly. I have fixed the issue. I am planning to increase the Maximum connection for the particular Queue Connection factory and i want to get the number of connection used/opened in the Queue Connection Factory via code, so that i can increase the maximum connections accordingly based on the traffic/volume. Any leads would be much helpful.
To learn the number of connections used and the number of queues opened by an application, you can use the MQSC DISPLAY CONN command like this:-
DISPLAY CONN(*) TYPE(ALL) ALL WHERE(OBJNAME EQ reply-q-name)
This will show you all the connections and all the open handles.
You can also discover exactly the same data using a programatic interface called PCF commands, although given how many excellent MQ admin tools there are out there, I'm not sure why you would need to do this "via code" as you put it?
For your second part of your question, how do you change the maximum connections based on load.
I have a some sample code using a datasource that may help to answer you question. Where I using name=built-in-derby-datasource, you can change the name to your Queue Connection factory name. If you need the lookup, change this jndi name jdbc/built-in-derby-datasource to your Queue Connection factory jndi name.
The code will get the admin client giving you access to queryMBeans. After you have the mbean, you can change maximum connections dynamically while the server is running.
#SuppressWarnings("unchecked")
public void AdminClientExample()
{
Object adminClient = null;
// Need to set the properties, type, host and port, defaults likely will work for most
Properties acProps = new Properties();
acProps.setProperty(AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_SOAP);
acProps.setProperty(AdminClient.CONNECTOR_HOST, "localhost");
acProps.setProperty(AdminClient.CONNECTOR_PORT, "8880");
// Set if security is enabled
//acProps.setProperty(AdminClient.CONNECTOR_SECURITY_ENABLED, "true");
//acProps.setProperty(AdminClient.USERNAME, "userid");
//acProps.setProperty(AdminClient.PASSWORD, "userid password");
try
{
adminClient = AdminClientFactory.createAdminClient(acProps);
}
catch (Exception e)
{
e.printStackTrace();
}
ObjectInstance obi = null;
ObjectName obn = null;
Set<ObjectInstance> s = null;
try {
// The two types to use are J2CConnectionFactory and DataSource if searching through a list of mbeans of that type.
// type=J2CConnectionFactory
// type=DataSource
// obn = new ObjectName("WebSphere:type=DataSource,*");
// s1 =((AdminClient)adminClient).queryMBeans(obn, null); // search through s1
// You can provide the name like this,
obn = new ObjectName("WebSphere:name=built-in-derby-datasource,*");
s =((AdminClient)adminClient).queryMBeans(obn, null);
// s should contain WebSphere:name=built-in-derby-datasource,process=server1,platform=dynamicproxy,node=DefaultNode01,JDBCProvider=Derby JDBC Provider (XA),diagnosticProvider=true,j2eeType=JDBCDataSource,J2EEServer=server1,Server=server1,version=9.0.0.11,type=DataSource,mbeanIdentifier=cells/DefaultCell01/resources.xml#DataSource_9007001,JDBCResource=Derby JDBC Provider (XA),cell=DefaultCell01,spec=1.0
} catch (Exception e) {
e.printStackTrace();
}
if (s == null) {
System.out.println("Did not find MBeans querying for object name " + obn.toString());
return;
} else {
obi = s.iterator().next();
}
// Normally the application using the connection pool will have
// already done the lookup which creates the objects
// required to change maxConnections. This lookup is only for
// this example.
InitialContext ctx;
try {
ctx = new InitialContext();
ctx.lookup("jdbc/built-in-derby-datasource");
} catch (Exception e) {
e.printStackTrace();
}
// show the connection pool contents
Object [] parms = null;
String [] parmsTypes = null;
try {
String ss = (String) ((AdminClient)adminClient).invoke(obi.getObjectName(), "showPoolContents", parms, parmsTypes);
System.out.println(ss);
} catch (Exception e) {
e.printStackTrace();
}
// get maxConnections
try {
Object maxConnections = ((AdminClient)adminClient).getAttribute(obi.getObjectName(), "maxConnections");
System.out.println(maxConnections);
} catch (Exception e) {
e.printStackTrace();
}
// change the maxConnections to 11,
try {
Integer it = new Integer(11);
Attribute at = new Attribute("maxConnections", it);
((AdminClient)adminClient).setAttribute(obi.getObjectName(), at );
} catch (Exception e) {
e.printStackTrace();
}
// show the connection pool contents, maxConnection now should be 11.
// or you can use the get maxConnection to check
// the changed value.
try {
String ss = (String) ((AdminClient)adminClient).invoke(obi.getObjectName(), "showPoolContents", parms, parmsTypes);
System.out.println(ss);
// or
Object maxConnections = ((AdminClient)adminClient).getAttribute(obi.getObjectName(), "maxConnections");
System.out.println(maxConnections);
} catch (Exception e) {
e.printStackTrace();
}
}
I am trying to run an Espresso test for my android app, but there is a problem that has been troubling me. In MainActivity, some views' visibility depends on data loaded from net, but in MainActivityTest, I can't manipulate the process of loading data, so I don't know the real data and which view should show and which view should not show. As a result, I don't know how to continue my test. Anyone can tell me how to handle this situation? Thanks!
Try using the MockWebServer library. It lets you mock http responses in your tests, like this:
/**
* Constructor for the test. Set up the mock web server here, so that the base
* URL for the application can be changed before the application loads
*/
public MyActivityTest() {
MockWebServer server = new MockWebServer();
try {
server.start();
} catch (IOException e) {
e.printStackTrace();
}
//Set the base URL for the application
MyApplication.sBaseUrl = server.url("/").toString();
//Create a dispatcher to handle requests to the mock web server
Dispatcher dispatcher = new Dispatcher() {
#Override
public MockResponse dispatch(RecordedRequest recordedRequest) throws InterruptedException {
try {
//When the activity requests the profile data, send it this
if(recordedRequest.getPath().startsWith("/users/self")) {
String fileName = "profile_200.json";
InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName);
String jsonString = new String(ByteStreams.toByteArray(in));
return new MockResponse().setResponseCode(200).setBody(jsonString);
}
//When the activity requests the image data, send it this
if(recordedRequest.getPath().startsWith("/users/self/media/recent")) {
String fileName = "media_collection_model_test.json";
InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName);
String jsonString = new String(ByteStreams.toByteArray(in));
return new MockResponse().setResponseCode(200).setBody(jsonString);
}
} catch (IOException e) {
e.printStackTrace();
}
return new MockResponse().setResponseCode(404);
}
};
server.setDispatcher(dispatcher);
}
We have a Web Application which will serve more than 1000 concurrent users
Currently , the Utility class for obtaining the DB Connnection is
public static Connection getDBConnection()
{
Connection conn = null;
try
{
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/MyLocalDB");
try
{
conn = ds.getConnection();
}
catch ( SQLException sqlEx )
{
System.out.println( "cannot get JDBC connection: " + sqlEx );
}
}
catch ( NamingException nEx )
{
nEx.printStackTrace();
}
return conn;
}
Option 2 :
public class DBConnection2 {
private static DataSource dataSource;
static {
try {
dataSource = (DataSource) new InitialContext().lookup("java:/comp/env/jdbc/MyLocalDB");
} catch (NamingException e) {
try {
throw new Exception("'jndifordbconc' not found in JNDI",e);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
public static Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
}
Please let me know whats the better option (I guess its second operation as lookup is a costly operation and i am doing it only for once in a application .)
please share your views .
You should adopt the second approach. As you say you don't need to keep looking up the datasource in JNDI.
I am assuming that you are using a pooled data source as provided by the tomcat-jdbc project, otherwise performance will be terrible.
On the second approach you should be sure to use connection pooling. Otherwise users will have to wait for others.
For example To Tomcat brings it own connection pool.
Or if you don't use Tomcat maybe you could have a look at C3p0: JDBC DataSources/Resource Pools.
see: Instantiating and Configuring a ComboPooledDataSource
The second option initialize dataSource only once when the class is loaded, then it is shared around all DBConnection2 instances. The first option initialize a new DataSource instance every time you call getDBConnection(). Thus the second option has better performance. But please be pay attention to connection release issue. Make sure close the connection if you choose the first option. I prefer use a framework to handle DB connection, like Spring JDBC: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jdbc.html
I have created a class that provides a method to obtain via REST a JSON object that should be then deserialised as LinkedHashMap. The method is the following:
#Override
public LinkedHashMap refreshStatus(String metricType, String metricPeriod) {
ObjectMapper mapper = new ObjectMapper();
LinkedHashMap<String,String> map = null;
try {
map = mapper.readValue(new File("http://localhost:8081/Metrics/Stats/JSONP/ReceiveData/"+metricType+"/"+metricPeriod+"/?callback="), LinkedHashMap.class);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return map;
}
When I enter in the browser the URL with some specific parameters, i.e.
http://localhost:8081/Metrics/Stats/JSONP/ReceiveData/Totals/All/?callback=
I get a proper JSON object:
{"Boxes":"8","Contracts":"23","Updates":"1","Deactivations":"4","ActiveBoxes":"33","InactiveBoxes":"1","PendingInstallationsContracts":"70","Alerts":"626","PercentsBoxInstalled":"100.0%","Miles":"0"}
However when I run the application and the method is invoked, I get a FileNotFoudException:
java.io.FileNotFoundException: http:/localhost:8081/Metrics/Stats/JSONP/ReceiveData/Totals/All/?callback= (No such file or directory)
Does anyone know where the problem may be? Thanks in advance!
This file indeed cannot be found on your computer. You try to create file objects correspoding to real file in your local file system and provide URL as an argument instead of file system path.
You actually want to perform HTTP GET, i.e. do something like the following.
urlString = "http://localhost:8081/Metrics/Stats/JSONP/ReceiveData/Totals/All/?callback=";
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
map = mapper.readValueconn.getInputStream();
I am trying to create a GUI-based program in Java that has a 'Submit' button and when clicked, it takes you to a website.
I have read about the URL and URLConnection classes online and the connection to the website is established but the program does not open the link... This is what I have so far:
if(command.equals("Submit data"))
{
try {
URL myURL = new URL("http://google.com/");
URLConnection myURLConnection = myURL.openConnection();
myURLConnection.connect();
}
catch (IOException t) {
// openConnection() failed
// ...
}
}
The connection seems to be established but I want the program to open up the browser and take to the website.. I've tried everything and no luck..
Thank you
You could either used a swing component like you can see in this thread --> Best Java/Swing browser component?
Otherwise use this snippet found at http://andy.ekiwi.de/?p=1026
public void openUrl(String url) throws IOException, URISyntaxException {
if(java.awt.Desktop.isDesktopSupported() ) {
java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
if(desktop.isSupported(java.awt.Desktop.Action.BROWSE) ) {
java.net.URI uri = new java.net.URI(url);
desktop.browse(uri);
}
}
}