I'm trying to create a java program that will edit the class path properties of the JVM on WebSphere. I am now able to connect the program to the server and also find the node, but I don't know what to do next. Which MBean should I use to be able to find the JVM Classpath and configure it? I know I can edit it on the AdminConsole itself, but for this task, I would really have to do it using a Java Program.
I am also not allowed to use wsadmin and jacl/jython scripts.
Here is my code so far:
public class Expi
{
private AdminClient adminClient;
private ObjectName nodeAgent;
private AdminService adminSF;
String hostName = "localhost";
String hostPort = "8880";
String nodeName = "node1";
String servName = "server1";
public static void main(String [] args) throws NamingException, AdminException
{
Expi xp = new Expi();
xp.createAdminClient();
xp.getNodeAgentMBean(nodeName);
xp.testMB();
}
private void createAdminClient()
{
// Set properties for the connection
Properties connectProps = new Properties();
connectProps.setProperty(
AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_SOAP);
connectProps.setProperty(AdminClient.CONNECTOR_HOST, hostName);
connectProps.setProperty(AdminClient.CONNECTOR_PORT, hostPort);
connectProps.setProperty(AdminClient.USERNAME, hostName);
connectProps.setProperty(AdminClient.CONNECTOR_SECURITY_ENABLED, "false");
// Connection Status Update
try
{
adminClient = AdminClientFactory.createAdminClient(connectProps);
System.out.println("Connected to SOAP Connection!");
}
catch (java.lang.Exception e)
{
System.out.println("Exception creating admin client: " + e);
System.exit(-1);
}
}
private void getNodeAgentMBean(String nodeName) throws NamingException
{
// Finding the specified node
try
{
String query = "WebSphere:type=Server,node="+ nodeName + ",*";
ObjectName queryName = new ObjectName(query);
Set s = adminClient.queryNames(queryName, null);
if (!s.isEmpty())
{
nodeAgent = (ObjectName)s.iterator().next();
System.out.println("Specified Node Found:" + nodeName);
}
else
{
System.out.println("Node agent MBean was not found");
System.exit(-1);
}
}
catch (MalformedObjectNameException e)
{
System.out.println(e);
System.exit(-1);
}
catch (java.lang.Exception e)
{
System.out.println(e);
System.exit(-1);
}
}
private void testMB() //find the correct MBean and use it to edit the values
{
AdminServiceFactory.getAdminService();
AdminServiceFactory.getMBeanFactory();
}
}
Any help would really be appreciated. Thank you!
You can execute the admin command using the AdminClient API. It is very difficult for to find out specific action since it is not well documented like JACL or JYTHON.
Sample available below for simple "listNodes" command. You can use the same approach to "listServers" and get the specific server object you want to modify. Once the server object is obtained, you can look at the same documentation below for executing command on specific target object.
AdminCommand cmd = cmdMgr.createCommand("listNodes");
cmd.setConfigSession(session);
AsyncCommandClient asyncCmdClientHelper =
new AsyncCommandClient(session, null);
asyncCmdClientHelper.execute(cmd);
CommandResult result = cmd.getCommandResult();
if (result.isSuccessful()) {
System.out.println("Successfully executed the command");
System.out.println("Result: ");
Object resultData = result.getResult();
if (resultData instanceof Object[]) {
Object[] resDataArr = (Object[])resultData;
for (Object resData : resDataArr) {
System.out.println(resData);
}
} else {
System.out.println(resultData);
}
} else {
System.out.println("Failed to execute the command");
result.getException().printStackTrace();
}
Documentation Link : https://www.ibm.com/developerworks/websphere/techjournal/1303_samantary/1303_samantary.html
Related
I have my client trying to lookup a JMS server. Here is my class JmsTest.java:
public static void main(String[] aInArgs)
{
boolean bContinue = true;
try
{
// determine JmsTest configuration based on command line arguments.
JmsTest jmsTest = parseCommandLine(aInArgs);
// connect to the server.
//jmsTest.initializeConnection();
Thread jmsFaultClientThread = null;
jmsFaultClientThread = new Thread("RUN") {
#Override
public void run() {
try
{
System.out.println("jmsFaultClient starting...");
jmsTest.initializeConnection();
}
catch (Exception e)
{
System.out.println("Exception: " + e.toString());
}
System.out.println("jmsFaultClient started.");
}
};
jmsFaultClientThread.start();
And my method initializeConnection():
public void initializeConnection() throws Exception
{
try
{
Hashtable env = new Hashtable();
env.put(Context.SECURITY_PRINCIPAL, user );
env.put(Context.SECURITY_CREDENTIALS, password);
jndiContext = new InitialContext(env);
System.out.println("Initializing Topic (" + strName + ")...");
try
{
topicConnectionFactory = (TopicConnectionFactory) jndiContext.lookup(CONNECTION_FACTORY);
}
catch (Exception e)
{
topicConnectionFactory = getExternalFactory(jndiContext);
}
When I run jmsTest.initializeConnection() like this everything works, and the lookup is working. However, the problem is when it's run inside the thread it gets stuck without any exception or any error. It's just stuck.
In my logs i'm seeing:
System.out.println("Initializing Topic (" + strName + ")...");
Which is a log inside my try / catch, and nothing else.
In dependencies, I have 2 jars, contening javax\jms. With the first one it's work inside the thread, and with the second one it doesn't. But I don't know why my jar can "block" the thread.
UPDATE 1 :
#AnotherJavaprogrammer said me to print the error:
here is my lookup with print :
try
{
getLogger().debug("TRY context");
Context lInitialContext = (Context) jndiContext.lookup(JMS_CONTEXT);
lInitialContext.lookup("SAMConnectionFactory");
getLogger().debug("END trying context");
}
catch (Exception e)
{
getLogger().debug("Catch");
getLogger().debug("Exception", e);
}
The output from getLogger().debug("END trying context") never comes, and I don't see the getLogger().debug("Catch") one either. So it appears I'm really "stuck" inside the lookup(). I can't go further, and it doesn't throw an exception.
I am using following code for registering and listening to Oracle database change notifications. This code is working fine when i run it as a standalone java program. It is receiving the notification from the database and printing as expected.
public class DBChangeNotification {
static final String USERNAME = "XXX";
static final String PASSWORD = "YYY";
static String URL = "jdbc:oracle:thin:#xxxx:xxxx:xxxx";
public static void main(String[] args) {
DBChangeNotification demo = new DBChangeNotification();
try {
demo.run();
} catch (SQLException mainSQLException) {
mainSQLException.printStackTrace();
}
}
public void run() throws SQLException {
OracleConnection conn = connect();
Properties prop = new Properties();
prop.setProperty(OracleConnection.DCN_NOTIFY_ROWIDS, "true");
prop.setProperty(OracleConnection.DCN_QUERY_CHANGE_NOTIFICATION, "true");
prop.setProperty(OracleConnection.DCN_BEST_EFFORT, "true");
DatabaseChangeRegistration dcr = conn.registerDatabaseChangeNotification(prop);
try {
// add the listenerr:
DCNDemoListener list = new DCNDemoListener(this);
dcr.addListener(list);
// second step: add objects in the registration:
Statement stmt = conn.createStatement();
// associate the statement with the registration:
((OracleStatement) stmt).setDatabaseChangeRegistration(dcr);
ResultSet rs = stmt.executeQuery("select * from xxxxxxxx where yyyy='zzzzz'");
while (rs.next()) {
}
String[] tableNames = dcr.getTables();
for (int i = 0; i < tableNames.length; i++) {
System.out.println(tableNames[i] + " is part of the registration.");
}
rs.close();
stmt.close();
} catch (SQLException ex) {
// if an exception occurs, we need to close the registration in order
// to interrupt the thread otherwise it will be hanging around.
if (conn != null) {
conn.unregisterDatabaseChangeNotification(dcr);
}
ex.printStackTrace();
throw ex;
} finally {
try {
// Note that we close the connection!
conn.close();
} catch (Exception innerex) {
innerex.printStackTrace();
}
}
}
/**
* Creates a connection the database.
*/
OracleConnection connect() throws SQLException {
OracleDriver dr = new OracleDriver();
Properties prop = new Properties();
prop.setProperty("user", DBChangeNotification.USERNAME);
prop.setProperty("password", DBChangeNotification.PASSWORD);
return (OracleConnection) dr.connect(DBChangeNotification.URL, prop);
}
}
/**
* DCN listener: it prints out the event details in stdout.
*/
class DCNDemoListener implements DatabaseChangeListener {
DBChangeNotification demo;
DCNDemoListener(DBChangeNotification dem) {
System.out.println("DCNDemoListener");
demo = dem;
}
#Override
public void onDatabaseChangeNotification(DatabaseChangeEvent e) {
Thread t = Thread.currentThread();
System.out.println("DCNDemoListener: got an event (" + this + " running on thread " + t + ")");
System.out.println(e.toString());
synchronized (demo) {
demo.notify();
}
}
}
My requirement is to use this feature in a web application. Web application when started in the server, has to listen to data change notifications (may be on a separate thread) and notify the application through a websocket client. I have added the following code in contextInitialized method of servlet context listener, so that it will start as soon as the application starts.
public class MyServletContextListener implements ServletContextListener {
DBChangeNotification demo;
#Override
public void contextDestroyed(ServletContextEvent arg0) {
//Notification that the servlet context is about to be shut down.
}
#Override
public void contextInitialized(ServletContextEvent arg0) {
demo = new DBChangeNotification();
try {
demo.run();
} catch (SQLException mainSQLException) {
mainSQLException.printStackTrace();
}
}
}
I did not see any notifications received by the web application when database change event occurs in the registered table. Please help me in resolving the issue. I do not know whether this is a correct approach or not.... may please suggest any alternative except continuous polling. I need to start something in the server as soon as i receive notification from database. Thank you.
It might be that you're running your code on an Oracle instance that doesn't have the Notification API available.
Check this SO for more info
When I execute my below code, it will read data from MQ on the console and then delete all the data from the queue. I don't want my data to be deleted from the queue while reading from MQ. I want to make it parameterize and also wants to write the data to excel. Can anybody please help me out from this. Below is my code that I am using.
public class MQReadJava
{
private MQQueueManager _queueManager = null;
public int port = 1416;
public String hostname = "xyz";
public String channel = "SYSTEM.ABC.123";
public String qManager = "ABC.BAW";
public String inputQName = "MYQUEUE";
public MQReadJava()
{
super();
}
private void init(String[] args) throws IllegalArgumentException
{
// Set up MQ environment
MQEnvironment.hostname = hostname;
MQEnvironment.channel = channel;
MQEnvironment.port = port;
}
public static void main(String[] args)throws IllegalArgumentException
{
MQReadJava readQ = new MQReadJava();
try
{
readQ.init(args);
readQ.selectQMgr();
readQ.read();
}
catch (IllegalArgumentException e)
{
System.exit(1);
}
catch (MQException e)
{
System.out.println(e);
System.exit(1);
}
}
private void selectQMgr() throws MQException
{
_queueManager = new MQQueueManager(qManager);
}
private void read() throws MQException
{
int openOptions = MQC.MQOO_INQUIRE + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_INPUT_SHARED;
//int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING;
MQQueue queue = _queueManager.accessQueue( inputQName,
openOptions,
null, // default q manager
null, // no dynamic q name
null ); // no alternate user id
System.out.println("MQRead is now connected.\n");
int depth = queue.getCurrentDepth();
System.out.println("Current depth: " + depth + "\n");
if (depth == 0)
{
return;
}
MQGetMessageOptions getOptions = new MQGetMessageOptions();
getOptions.options = MQC.MQGMO_NO_WAIT + MQC.MQGMO_FAIL_IF_QUIESCING +
MQC.MQGMO_CONVERT;
while(true)
{
MQMessage message = new MQMessage();
try
{
queue.get(message, getOptions);
byte[] b = new byte[message.getMessageLength()];
message.readFully(b);
System.out.println(new String(b));
message.clearMessage();
}
catch (IOException e)
{
System.out.println("IOException during GET: " + e.getMessage());
break;
}
catch (MQException e)
{
if (e.completionCode == 2 && e.reasonCode == MQException.MQRC_NO_MSG_AVAILABLE) {
if (depth > 0)
{
System.out.println("All messages read.");
}
}
else
{
System.out.println("GET Exception: " + e);
}
break;
}
}
queue.close();
_queueManager.disconnect();
}
}
Below is my code that I am using.
:) You downloaded my MQRead program. If you don't want it to do a destructive MQGET then you need to update the code to do a browse (see JoshMc's comments). Why didn't you download my MQBrowse program? Finally, please start reading the MQ Knowledge Center as it contains lots & lots of information for beginners to IBM MQ.
Update: You should not use MQEnvironment class as it is not thread safe. Put the connection information in a Hashtable. See here for an example:
Java program to connect WMQ with User Id instead of channel
In order to not delete messages from the queue you need to browse the queue, this would be accomplished by updating your openOptions and getOptions as follows:
int openOptions = MQC.MQOO_INQUIRE + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_BROWSE;
and
getOptions.options = MQC.MQGMO_NO_WAIT + MQC.MQGMO_FAIL_IF_QUIESCING + MQC.MQGMO_CONVERT + MQC.MQGMO_BROWSE_NEXT;
Checkout this great list of sample IBM MQ Classes for Java applications on Capitalware's website: Sample IBM MQ Java Code
i am having a trouble when trying to run communication between server and client using RMI. My code is attached below. Server runs fine, i can see listening port in list of pc ports, client fails on registry.lookup. I didnt find any similar problem. Please help.
Server:
public class Main {
private static int port = Registry.REGISTRY_PORT;
private static Registry registry;
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
if (args.length == 1) {
try {
port = Integer.parseInt(args[0]);
}
catch (NumberFormatException e) {
System.out.println("Port number is not valid!");
System.exit(0);
}
}
if (System.getSecurityManager() == null)
System.setSecurityManager(new RMISecurityManager());
String name = "DBInterface";
System.out.println("Starting server.");
try {
DBInterface DBInterface = new DBClass();
DBInterface stub = (DBInterface) UnicastRemoteObject.exportObject(DBInterface, 5000);
registry = LocateRegistry.createRegistry(port);
registry.rebind(name, stub);
}
catch (RemoteException e)
{
System.out.println(e.getMessage());
}
System.out.println("Server succesfully started.");
}
}
Client:
public class Client {
private static String ipAddress = "localhost";
private static int port = Registry.REGISTRY_PORT;
private static String confFile = "";
public static DBInterface dbServer = null;
public static String name = "DBInterface";
public static void main(String[] args) {
System.out.println("Starting client.");
if (System.getSecurityManager() == null)
System.setSecurityManager(new RMISecurityManager());
if (args.length == 3) {
try {
ipAddress = args[0];
port = Integer.parseInt(args[1]);
confFile = args[2];
}
catch (NumberFormatException e) {
System.out.println("Port number is not valid!");
System.exit(0);
}
}
else
{
System.out.println("Some parameter is missing!");
System.out.println("Valid parameters are: IP(0) Port(1) Config_file_name(2).");
}
ArrayList<ArrayList<String>> commands = Tools.loadConfigFile(confFile);
Iterator<ArrayList<String>> commandsIterator = commands.iterator();
Registry registry;
try {
registry = LocateRegistry.getRegistry(ipAddress, port);
dbServer = (DBInterface) registry.lookup(name); //code fails here
} catch (RemoteException e) {
System.out.println(e.getMessage()); //"no such object in table" is printed on the screen
} catch (NotBoundException e) {
System.out.println(e.getMessage());
}
Tools tools = new Tools(dbServer);
...
What you describe is basically impossible. That exception means that the stub you are using, in this case a Registry, no longer refers to a live exported object in the target JVM. However as Registry stubs are created locally there is no opportunity for them to expire at all. The only mechanism would be the Registry you created exiting, but the static Registry reference should prevent that.
I would change a few things though:
DBInterface stub = (DBInterface) UnicastRemoteObject.exportObject(DBInterface, 5000);
registry = LocateRegistry.createRegistry(port);
Reverse these two lines, and use port for both the Registry and the export. You don't need two ports. Also, in all your catch blocks, never just suppress the exception message in favour of your own. You should always at least log the actual exception message.
I'm just getting started with RMI and I'm trying to write a simple program that simulates a train booking system. I have the basics set up - Server, Client, and a Remote object exported. It works fine with one Client connection. However when more than 1 Client connects, the Clients seem to be executing in the same thread. This is the case when I run multiple Clients on the same machine or when I connect a Client from another laptop.
I was under the impression that RMI handled threading on the server side? If not, how do I go about handling multiple Client connections given the code below?
Here are the classes of interest.
Server.....
public class Server {
public Server() {
try {
Booking stub = (Booking) UnicastRemoteObject.exportObject(new BookingProcess(), 0);
Registry registry = LocateRegistry.getRegistry();
registry.bind("Booking", stub);
System.err.println("Server Ready");
} catch (RemoteException e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
} catch (AlreadyBoundException e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
public static void main(String[] args) {
Server server = new Server();
}
}
BookingProcess.....(I've left out the private methods that processInput(String input) uses)
public class BookingProcess implements Booking {
private static Journey dublinGalway = new Journey("Dublin to Galway");
private static Journey dublinLimerick = new Journey("Dublin to Limerick");
private static Journey dublinCork = new Journey("Dublin to Cork");
private Journey currentJourney;
private enum State {
INITIAL, JOURNEYS_DISPLAYED, JOURNEY_CHOSEN, ANOTHER_BOOKING_OFFERED, SOLD_OUT;
}
private State currentState = State.INITIAL;
public synchronized String processInput(String input) {
String output = "";
if(currentState == State.INITIAL) {
if(bookedOut()) {
output = "Sorry, there are no seats remaining on any route. Get the bus.";
currentState = State.SOLD_OUT;
}
else {
output = "Please choose a journey to book: " + "1: " + dublinGalway.getDescription() + ", 2: " + dublinLimerick.getDescription() + ", 3: " + dublinCork.getDescription();
currentState = State.JOURNEYS_DISPLAYED;
}
}
else if(currentState == State.JOURNEYS_DISPLAYED) {
output = this.processJourneyChoice(input);
}
else if(currentState == State.JOURNEY_CHOSEN) {
output = "Do you wish to confirm this booking? (y/n)";
if(input.equalsIgnoreCase("y")) {
if(bookingConfirmed()) {
output = "Thank you. Your journey from " + currentJourney.getDescription() + " is confirmed. Hit return to continue.";
//currentState = State.ANOTHER_BOOKING_OFFERED;
}
else {
output = "Sorry, but the last seat on the " + currentJourney.getDescription() + " route has just been booked by another user.";
//currentState = State.ANOTHER_BOOKING_OFFERED;
}
currentState = State.ANOTHER_BOOKING_OFFERED;
}
else if(input.equalsIgnoreCase("n")) {
output = "You have cancelled this booking. Hit return to continue.";
currentState = State.ANOTHER_BOOKING_OFFERED;
}
}
else if(currentState == State.ANOTHER_BOOKING_OFFERED) {
output = "Would you like to make another booking? (y/n)";
if(input.equalsIgnoreCase("y")) {
output = "Hit Return to continue.";
currentState = State.INITIAL;
}
else if(input.equalsIgnoreCase("n")){
output = "Goodbye.";
try {
Thread.currentThread().join(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
currentState = State.INITIAL;
}
}
else if(currentState == State.SOLD_OUT) {
output = "Goodbye.";
}
return output;
}
And finally Client......
public class Client {
public static void main(String[] args) {
Client client = new Client();
client.runClient();
}
public void runClient() {
try {
BufferedReader consoleInput = new BufferedReader(new InputStreamReader(System.in));
Registry registry = LocateRegistry.getRegistry("localhost");
Booking stub = (Booking) registry.lookup("Booking");
String serverResponse = stub.processInput("begin");
System.out.println("Server: " + serverResponse);
while((serverResponse = stub.processInput(consoleInput.readLine())) != null) {
System.out.println(serverResponse);
if(serverResponse.equals("Goodbye.")) {
break;
}
}
} catch (Exception e) {
System.err.println("Client exception " + e.toString());
e.printStackTrace();
}
}
}
As for as RMI server threads, the answer is that it may or may not run in a separate thread. See the documentation here:
http://docs.oracle.com/javase/6/docs/platform/rmi/spec/rmi-arch3.html
3.2 Thread Usage in Remote Method Invocations
A method dispatched by the RMI runtime to a remote object implementation may or may not execute in a separate thread. The RMI runtime makes no guarantees with respect to mapping remote object invocations to threads. Since remote method invocation on the same remote object may execute concurrently, a remote object implementation needs to make sure its implementation is thread-safe.
You can take server side thread dumps and you would see that the RMI TCP Connection threads IDs keep changing, however as #jtahlborn noticed the server side method is synchronized so it would execute serially, not necessarily in a single thread though.
Your server side processInput() method is synchronized, so, yes, the calls will be handled serially. what does that have to do with RMI?
UPDATE:
if you want to have separate currentState and currentJourney values for each client session, then you need to use the RMI remote session pattern, see this answer for details.