Is it feasible to check queue depth(MQ) using any scripts? [No restrictions on the language]. The plan is to look at non-Java solutions.
I do understand that it is achievable in Java using MQQueueManager but that would need the usage of client API. Hence checking for any alternate options or better practices.
InquireQueue at http://www.capitalware.biz/mq_code_perl_python.html looks similar[but looks a bit outdated]
Didn't Google give you a recent blog posting I wrote called "How to Clear a MQ Queue from a Script or Program" at http://www.capitalware.biz/rl_blog/?p=1616
Just change the MQSC "clear" command to "current depth" (CURDEPTH).
i.e.
DIS QL(TEST.*) CURDEPTH
Does nobody use google anymore ?
PyMQI, an open-source Python extension for WebSphere MQ
http://metacpan.org/pod/MQSeries::Queue
my %qattr = $queue->Inquire( qw(MaxMsgLength MaxQDepth) );
The perl mqseries is very complete. Below is some sample code. (Part of the credit for the sample probably goes to someone else, but it has been floating around my drive for years.) The code connects to the queue manager specified by the command line, if not supplied, it will connect to the default queue manager. It then inquires about the queue name passed in, specifically, the current depth of that queue. This is displayed to the user. This code can easily be modified to display other queue properties. Furthermore, MQINQ can be used to inquire about the attributes of other objects, not just queues.Here is the subset sample code:
use MQSeries;
my $quename = $ARGV[0];
my $quemgrname = $ARGV[1];
my $Hconn = MQCONN($qmgrname, $CompCode, $Reason);
print"MQCONN reason:$Reason\n";
my $ObjDesc = { ObjectType => MQOT_Q, ObjectName => $qname };
my $Options = MQOO_INQUIRE | MQOO_SET | MQOO_FAIL_IF_QUIESCING;
my $Hobj = MQOPEN($Hconn,$ObjDesc,$Options,$CompCode,$Reason);
print"MQOPEN reason:$Reason\n";
my $tst = MQINQ($Hconn,$Hobj,$CompCode,$Reason,MQIA_CURRENT_Q_DEPTH);
print"Depth of $qname is: $tst\n";
MQCLOSE($Hconn,$Hobj,$COptions,$CompCode,$Reason);
print"MQCLOSE reason:$Reason\n";
MQDISC($Hconn,$CompCode,$Reason);
print"MQDISC reason:$Reason\n";
If you are logged in using MQM user on linux and want to have a quick check on queues with messages in them .. here is a quickfix ..
echo "dis ql(*) CURDEPTH" | runmqsc <QMGRNAME> | grep -v '(0' | grep -v 'AMQ'
this will give you a command line output and you can schedule the same command in crontab if needed directly ( without having to save a script for it )
I know its not neat but may be the quickest of solutions.
There's the many JVM based scripting/ish languages that give you access to Java classes. Some need a thin glue layer, some need nothing at all.
Groovy
Jython
Scala
Clojure
etc.
Related
I want to check whether JVM options for a particular application (in this case, Matlab) have been set to prefer IPV4 or if they still use IPV6.
I know how to set the JVM to prefer IPV4. In my case, it can be done by adding the line
-Djava.net.preferIPv4Stack=true
to the java.opts file within $MATLABROOT/bin/maci64/.
I can also check whether this line has already been added to java.opts via string-matching. I've pasted my current solution (a Matlab script that checks for string-match, and adds the line if it does not exist) at the bottom of this question.
I don't know how, though, to check whether IPV4 or IPV6 is preferred without string-matching. Obviously this seems preferred.
Does anybody know how to check IPV4 vs. IPV6 in the JVM without string-matching?
Here's my current solution, that depends on string-matching:
% OSX platform-specific: revert to IPv4
if (computer('arch') == 'maci64')
javaoptspath = fileread([matlabroot '/bin/' computer('arch') '/java.opts']);
k = strfind(javaoptspath, '-Djava.net.preferIPv4Stack=true');
if isempty(k)
setenv('DRAKE_IPV4_SET_MATLABROOT', matlabroot)
setenv('DRAKE_IPV4_SET_ARCH', computer('arch'))
display('Since you are on Mac, we will need to set your JVM to prefer IPV4 instead of IPV6 for MATLAB')
display('Please enter your sudo password below')
! (echo "" | echo "-Djava.net.preferIPv4Stack=true") | sudo tee -a $DRAKE_IPV4_SET_MATLABROOT/bin/$DRAKE_IPV4_SET_ARCH/java.opts
end
end
You can access the underlying java system properties without parsing the options string by using the java.lang.System class directly from Matlab.
For example:
ipv4_preferred = java.lang.System.getProperty('java.net.preferIPv4Stack')
The result of getProperty will be empty if the user has not set -Djava.net.preferIPv4Stack=..., so a more complete solution might be:
ipv4_preferred = java.lang.System.getProperty('java.net.preferIPv4Stack');
if isempty(ipv4_preferred)
ipv4_preferred = false;
end
I am calling a jar via perl with the following command.
my $command = "$java_home/bin/java my_jar.jar ARG1 ARG2 ARG3";
my $result = `$command 2>&1;
However my JAR also expects arguments via STDIN. I need to know how to pass those arguments. I have tried passing them like normal arguments, and that didn't work. I read on a forum that OPEN2 might work however after reading the documentation I couldn't figure out how to make it work.
Any ideas on how to make this work would be great.
Thanks ahead of time.
Since you need to send and receive data from the Java process, you need two-way communication. That's what IPC::Open2 is designed to do. This allows you to create a dedicated pipe that renders STDIN/STDOUT unnecessary:
use IPC::Open2;
my $pid = open2( \*from_jar, \*to_jar, $command )
or die "Could not open 2-way pipe: $!";
print to_jar, "Here is input\n"; # Pass in data
my $result = <from_jar>; # Retrieve results
Also consider IPC::Open3 to handle errors as well.
i want to take backup of postgres database using java. I am using following code for this
but this is not working and not generating dump.
String pgDump = "C:\\Program Files\\PostgreSQL\\9.2\\bin\\pg_dump";
String dumpFile = "D:\\test\\"+ tenant.getTenantAsTemplate()+".sql";
String sql = pgDump+" -h localhost -U postgres -P postgres " + tenant.getTenantAsTemplate()+" > "+dumpFile;
Process p = Runtime.getRuntime().exec(sql);
int time = p.waitFor();
System.out.println("time is "+time);
if(time == 0){
System.out.println("backup is created");
}
else{
System.out.println("fail to create backup");
}
Here i am getting time is 1.
This is also operating system dependent and we need also pg_dump. is there any other way to generate backup of database without pg_dump?
please reply soon.
No, there is no way to generate a database backup without pg_dump, using the regular SQL connection. It's a bit of an FAQ, but the people who want the feature never step up to do the work to implement the feature in PostgreSQL.
I guess technically you could use a replication connection to do a physical base backup like pg_basebackup does, but that's not really what you want, requires copying all databases on the machine, and would be a lot of work.
You should use the String[] form of Runtime.exec as I mentioned in a related answer regarding pg_restore.
You must also check the process exit value to see if it terminated successfully or not, and you must be careful to handle, not just swallow, any exceptions thrown.
Your code fails to check the exit value, and I think it's probably generating a malformed command that's failing with a non-zero exit code, probably because you are not correctly quoting the path to pg_dump. To see what's wrong, print the final assembled command line, you'll see something like:
C:\Program Files\PostgreSQL\9.2\bin\pg_dump -h localhost ....
which cmd.exe will split into:
c:\Program
Files\postgresql\9.2\bin\pg_dump
-h
localhost
... etc
See the problem?
Do not just quote the path to pg_dump to work around this. Use the String[] form of exec and you won't have to, plus it'll work correctly for other things like accidental %environmentvars% in paths.
I need to get a list of chunks after sharding inside my Java code. My code is simple and looks like this:
Mongo m = new Mongo( "localhost" , 27017 );
DB db = m.getDB( "admin" );
Object cr = db.eval("db.printShardingStatus()", 1);
A call of eval() returns an error:
Exception in thread "main" com.mongodb.CommandResult$CommandFailure: command failed [$eval]: { "serverUsed" : "localhost/127.0.0.1:27017" , "errno" : -3.0 , "errmsg" : "invoke failed: JS Error: ReferenceError: printShardingStatus is not defined src/mongo/shell/db.js:891" , "ok" : 0.0}
at com.mongodb.CommandResult.getException(CommandResult.java:88)
at com.mongodb.CommandResult.throwOnError(CommandResult.java:134)
at com.mongodb.DB.eval(DB.java:340)
at org.sm.mongodb.MongoTest.main(MongoTest.java:35)
And, really, if we look into the code of db.js, in line 891 there is a call to a method printShardingStatus() that is not defined inside a file. Inside of sh.status() method in utils_sh.js file, there is even a comment:
// TODO: move the actual commadn here
Important to mention, when I run these commands in mongo command line, everything works properly!
My questions are:
Is there any other possibility of getting a full sharding status within Java code? (eg. with DB.command() method)
If not, any other suggestions how to avoid my problem?
Many of the shell's helper functions are not available for server-side code execution. In the case of printShardingStatus(), it makes sense because there isn't a console to use for printing output and you'd rather have a string returned. Thankfully, you should be able to pull up the source of the shell function and reimplement it in your application (e.g. concatenating a returned string instead of printing directly).
$ mongo
MongoDB shell version: 2.2.0
connecting to: test
> db.printShardingStatus
function (verbose) {
printShardingStatus(this.getSiblingDB("config"), verbose);
}
So, let's look at the printShardingStatus() function...
> printShardingStatus
function (configDB, verbose) {
if (configDB === undefined) {
configDB = db.getSisterDB("config");
}
var version = configDB.getCollection("version").findOne();
// ...
}
Before turning all of the output statements into string concatenation, you'd want to make sure the other DB methods are all available to you. Performance-wise, I think the best option is to port the innards of this function to Java and avoid server-side JS evaluation altogether. If you dive deeper into the printShardingStatus() function, you'll see it's just issuing find() on the config database along with some group() queries.
If you do want to stick with evaluating JS and would rather not keep this code within your Java application, you can also look into storing JS functions server-side.
Have you deployed a shard cluster properly?
If so, you could connect to a mongo database that has sharding enabled.
Try calling the method db.printShardingStatus() with a that database within the mongo shell and see what happens.
Apparently the Javascript function 'printShardingStatus' is only available for the mongo shell and not for execution with server commands, to see the code start mongo.exe and type only 'printShardingStatus' and press enter.
In this case writing an extension method would be the best for solving this...
Javascript way of printing output of MongoDB query to a file
1] create a javascript file
test.js
cursor = db.printShardingStatus();
while(cursor.hasNext()){
printjson(cursor.next());
}
2] run
mongo admin --quiet test.js > output.txt
We are working with some legacy code that accesses a shared drive by the letter (f:\ for example). Using the UNC notation is not an option. Our Java wrapper app will run as a service, and as the first step, I would like to map the drive explicitly in the code. Has anyone done this?
Consider executing the DOS command that maps a network drive as in the following code:
String command = "c:\\windows\\system32\\net.exe use f: \\\\machine\\share /user:user password";
Process p = Runtime.getRuntime().exec(command);
...
See details on net use command:
The syntax of this command is:
NET USE
[devicename | *] [\\computername\sharename[\volume] [password | *]]
[/USER:[domainname\]username]
[/USER:[dotted domain name\]username]
[/USER:[username#dotted domain name]
[/SMARTCARD]
[/SAVECRED]
[[/DELETE] | [/PERSISTENT:{YES | NO}]]
NET USE {devicename | *} [password | *] /HOME
NET USE [/PERSISTENT:{YES | NO}]
You can use JCIFS
http://jcifs.samba.org/src/docs/api/jcifs/smb/SmbFile.html
or if you want higher level API and support for other protocols like FTP, Zip and others:
http://commons.apache.org/vfs/filesystems.html
Both options are pure Java and cross platform.
I think the easiest way is to use the Runtime.getRuntime().exec() method and call the "net use" command.
For example:
try {
// Execute a command without arguments
String command = "C:\\Windows\\system32\\net.exe use F: \\\\server\\share /user:user password";
Process child = Runtime.getRuntime().exec(command);
} catch (IOException e) {
}