Skip to content

Instantly share code, notes, and snippets.

@jeansymolanza
Last active September 3, 2024 15:03
Show Gist options
  • Select an option

  • Save jeansymolanza/3bad5c5ff6ec049d96a3c12bcb810ac1 to your computer and use it in GitHub Desktop.

Select an option

Save jeansymolanza/3bad5c5ff6ec049d96a3c12bcb810ac1 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use MQClient::MQSeries;
sub printLog2 {
my ($message, $detail) = @_;
print "$message: $detail\n";
}
sub mq_conn {
my ($q_mgr_name) = @_;
my $hconn = 0;
my $comp_code = 0;
my $reason = 0;
my $success = 1;
# Retrieve environment variables
my $channel = $ENV{SSL_MQ_CHANNEL} // '';
my $ssl_cipher_spec = $ENV{SSL_CIPHER_SPEC} // '';
my $connection_name = $ENV{SSL_MQ_CONNECTION} // '';
my $ssl_key_repository = $ENV{MQSSLKEYR} // '';
# Prepare the client connection options
my $ClientConn = {
'Version' => 7,
'ChannelName' => $channel,
'TransportType' => 'TCP',
'SSLCipherSpec' => $ssl_cipher_spec,
'ConnectionName'=> $connection_name,
};
my $SSLConfig = {
KeyRepository => $ssl_key_repository,
};
# Call MQCONNX to connect to the queue manager
$hconn = MQClient::MQSeries::MQCONNX(
$q_mgr_name,
{ 'ClientConn' => $ClientConn, 'SSLConfig' => $SSLConfig },
\$comp_code,
\$reason
);
# Check the completion code
if ($comp_code != MQClient::MQSeries::MQCC_OK) {
printLog2("MQCONNX failed. Completion Code: $comp_code, Reason Code: $reason, Queue Manager Name", $q_mgr_name);
$success = 0;
}
return ($hconn, $success, $reason);
}
sub mq_open {
my ($hconn, $queue_name, $open_options) = @_;
my $hobj = 0;
my $comp_code = 0;
my $reason = 0;
# Define the object descriptor for the queue
my $od = {
'ObjectName' => $queue_name,
'ObjectQMgrName' => '',
'ObjectType' => MQClient::MQSeries::MQOT_Q,
};
# Open the queue
$hobj = MQClient::MQSeries::MQOPEN(
$hconn,
$od,
$open_options,
\$comp_code,
\$reason
);
# Check the completion code
if ($comp_code != MQClient::MQSeries::MQCC_OK) {
printLog2("MQOPEN failed. Completion Code: $comp_code, Reason Code: $reason, Queue Name", $queue_name);
return (0, $comp_code, $reason);
}
return ($hobj, $comp_code, $reason);
}
# Main script
my $q_mgr_name = "YOUR_QUEUE_MANAGER_NAME"; # Replace with your queue manager name
my $queue_name = "YOUR_QUEUE_NAME"; # Replace with your queue name
my $open_options = MQClient::MQSeries::MQOO_INPUT_AS_Q_DEF | MQClient::MQSeries::MQOO_OUTPUT;
# Connect to the Queue Manager
my ($hconn, $success, $reason) = mq_conn($q_mgr_name);
if (!$success) {
print "Failed to connect to Queue Manager. Exiting...\n";
exit 1;
}
print "Successfully connected to Queue Manager: $q_mgr_name\n";
# Open the Queue
my ($hobj, $open_comp_code, $open_reason) = mq_open($hconn, $queue_name, $open_options);
if ($open_comp_code != MQClient::MQSeries::MQCC_OK) {
print "Failed to open queue: $queue_name. Exiting...\n";
MQClient::MQSeries::MQDISC($hconn, \$comp_code, \$reason);
exit 1;
}
print "Successfully opened Queue: $queue_name\n";
# Continue with other MQ operations...
# Close the queue
MQClient::MQSeries::MQCLOSE($hobj, MQClient::MQSeries::MQCO_NONE, \$comp_code, \$reason);
if ($comp_code != MQClient::MQSeries::MQCC_OK) {
printLog2("MQCLOSE failed. Completion Code: $comp_code, Reason Code: $reason", $queue_name);
} else {
print "Successfully closed Queue: $queue_name\n";
}
# Disconnect from the Queue Manager
MQClient::MQSeries::MQDISC($hconn, \$comp_code, \$reason);
if ($comp_code != MQClient::MQSeries::MQCC_OK) {
printLog2("MQDISC failed. Completion Code: $comp_code, Reason Code: $reason", $q_mgr_name);
} else {
print "Successfully disconnected from Queue Manager: $q_mgr_name\n";
}
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment