IBM®
Skip to main content
    Country/region [select]      Terms of use
 
 
     Home      Products      Services & solutions      Support & downloads      My account     
  IBM Wikis > PHP: Zend for i5/OS > ... > 06-00-00 Chapter 6. Java Bridge support > 06-03-00 Sample programs
PHP: Zend for i5/OS Log In | Sign Up   View a printable version of the current page.
06-03-00 Sample programs
Added by admin, last edited by admin on Mar 01, 2007  (view change)
Labels: 
(None)

Show/Hide Navigation Tree
Navigation Tree

TOC Previous Next

Sample programs

This section contains some simple programs to show how to test and take advantage of the Java Bridge functionality found in Zend Platform for i5/OS.

Simple test program

The program in Example 6-1 shows how to get all of the Java system properties and iterate through them, printing all key/value pairs out to the screen.

Example 6-1 Print all Java system properties

<html>
<head><title>Java System Properties</title></head>
<?php

// Get java.lang.System instance
$system = new Java("java.lang.System");

// Get the properties.
// In PHP this will be put into an array of key-value pairs
$properties = $system->getProperties();

// Iterate through properties and write out to screen
foreach($properties as $key=>$value) {
 echo "<br>";
 echo $key . " : " . $value;
}

?> 

</html>

The output from running this PHP program looks similar to Figure 6-2.

Figure 6-2 Output from running Java system properties program

IBM Toolbox for Java and PHP

This section shows two sample programs that access i5/OS resources via the IBM Toolbox for Java.

Restriction: Only a few Toolbox functions were executed when this chapter was written. Comprehensive testing of all functionality has not been done.
For example, at the time this chapter was written the ability to run commands on i5/OS via the ibm.com .as400.access.CommandCall class did not work.

Message queue test program

Example 6-2 shows a simple program to display the contents of an i5/OS message queue. You should first make sure that there are messages in the queue to see output.

Example 6-2 Display all messages in i5/OS message queue

<html>
<head><title>Message Queue Test</title></head>
<?php

// Get an AS400 connection object
$as400 = new Java('com.ibm.as400.access.AS400', 'localhost', 'myuserid', 'mypassword');

// Create the MessageQueue object
$mq = new Java('com.ibm.as400.access.MessageQueue', $as400, '/QSYS.LIB/QUSRSYS.LIB/GARYMU.MSGQ');

// Get the number of messages in the queue and print it out
$numMessages = $mq->getLength();
echo '<br>Number of messages: ' . $numMessages ;

// If we have messages, print each; otherwise indicate no messages exist
if (0 < $numMessages) {
 $messages = $mq->getMessages(-1, 0);

 foreach($messages as $value) {
  echo '<br>Message = ' . $value;
 }
} else {
 echo '<br>No messages in queue.';
}

// Disconnect all services
$as400->disconnectAllServices();

?> 

</html>

The output from this program looks similar to Figure 6-3 (if there are messages in the queue).

Figure 6-3 Output from running Java Toolbox message queue program

Data queue test program

Example 6-3 shows how to post and peek entries from an i5/OS data queue. We test for the existence of the data queue - if it doesn't exist, we create it. If it does, we clear its contents.

Example 6-3 Manipulate i5/OS data queue

<html>
<head><title>Data Queue Test</title></head>
<?php

// Get an AS400 connection object
$as400 = new Java('com.ibm.as400.access.AS400', 'localhost', 'myuserid', 'mypassword');

// Get a data queue object
$dq = new Java('com.ibm.as400.access.DataQueue', $as400, "/QSYS.LIB/GARYMU.LIB/GARYDATAQ.DTAQ");

// Create a data queue object in i5/OS if it doesn't exist; otherwise clear its contents
if (!$dq->exists()) {
 $dq->create();
} else {
 $dq->clear();
}

// Write a string to the data queue containing the current time
$dq->write("Current date and time: " . date("F j, Y, g:i a"));

// Peek the data - this does not remove the entry from the data queue
$entry = $dq->peek();
$data = $entry->getString();

// Write out the data to the browser
echo '<br>Data queue data: ' . $data;

// Delete the data queue
$dq->delete();

// Disconnect all services
$as400->disconnectAllServices();
?> 

</html>

Figure 6-4 shows the output from the program in Example 6-3.

Figure 6-4 Output from running Java Toolbox data queue program

TOC Previous Next

Top 5 contributors
to this page
UserEdits
admin 2
The postings on this site solely reflect the personal views of the authors and do not necessarily represent the views, positions, strategies or opinions of IBM or IBM management.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.10 Build:#528 Nov 29, 2006)
    About IBM Privacy Contact