Windows service starts and exits immediately - asp.net

I have written a windows service which in turn calls a web service. When I run the windows service from a test app., it works perfectly. However when I install the service and then start it, it stops almost immediately. The only two entries I see in the log are Constructor and Thread Started. Not sure what is wrong.
public partial class WindowsService : ServiceBase
{
public LogManager.LogFile _log;
public Thread m_thread;
protected TimeSpan m_delay;
CommonFunctions _cf = new CommonFunctions();
DBFunctions _db = new DBFunctions();
public WindowsService()
{
InitializeComponent();
_log = new LogManager.LogFile(#"c:\test\servicelog.txt", true, true);
_log.WriteToLog("Constructor", LogLevel.Level0);
}
protected override void OnStart(string[] args)
{
m_delay = new TimeSpan(0,0,300);
base.OnStart(args);
try
{
m_thread = new System.Threading.Thread(Execute);
m_thread.Start();
_log.WriteToLog("Thread Started", LogLevel.Level0);
}
catch (Exception ex)
{ _log.WriteToLog(ex.Message, LogLevel.Level0); }
}
public void Execute()
{
_log.WriteToLog("Begin Execute...", LogLevel.Level0);
try
{
ProcessNewLMSUsers();
}
catch (Exception ex)
{
_log.WriteToLog(ex.Message.ToString());
}
}
private void ProcessNewLMSUsers()
{
try
{
_log.WriteToLog("Begin: Processing new LMS Users", LogLevel.Level1);
// Check for new users in the LMS.
string callErrorText = "";
bool userAdded = false;
LMSWS.SSO lms = _cf.GetLMSSSOWS(); **// this is a web service**
lms.Timeout = 99999;
}
REST OF THE CODE.................
}

I can't see there is anything wrong with your code. but you can try to put a "Thread.Sleep(20000); " code at the begining of OnStart method. e.g.
protected override void OnStart(string[] args)
{
Thread.Sleep(20000);
m_delay = new TimeSpan(0,0,300); //set a break-point here
base.OnStart(args);
try
{
m_thread = new System.Threading.Thread(Execute);
m_thread.Start();
_log.WriteToLog("Thread Started", LogLevel.Level0);
}
catch (Exception ex)
{ _log.WriteToLog(ex.Message, LogLevel.Level0); }
}
and once you start this service program in Windows Service, then you have to quickly attach you source code to the service program. In visual studio, it's menu "Debug" -> "Attach to Process...". then you can set break-point in your source code anywhere to check what's going wrong.

Related

Open and close channel in the gRPC client with every request

I have a gRPC client in a kafka application. This means the client will constantly open and close channels.
public class UserAgentClient {
protected final Logger logger = LoggerFactory.getLogger(getClass());
private static final Config uaparserConfig = ConfigFactory.load().getConfig(ua);
private final ManagedChannel channel;
private final UserAgentServiceGrpc.UserAgentServiceBlockingStub userAgentBlockingStub;
public UserAgentParserClient() {
this(ManagedChannelBuilder.forAddress(uaConfig.getString("host"), uaConfig.getInt("port")).usePlaintext());
}
public UserAgentClient(ManagedChannelBuilder<?> usePlaintext) {
channel = usePlaintext.build();
userAgentBlockingStub = UserAgentServiceGrpc.newBlockingStub(channel);
}
public UserAgentParseResponse getUserAgent(String userAgent ) {
UserAgentRequest request = UserAgentRequest.newBuilder().setUserAgent(userAgent).build();
UserAgentParseResponse response = null;
try {
response = userAgentBlockingStub.parseUserAgent(request);
} catch(Exception e) {
logger.warn("An exception has occurred during gRPC call to the user agent.", e.getMessage());
}
shutdown();
return response;
}
public void shutdown() {
try {
channel.shutdown();
} catch (InterruptedException ie) {
logger.warn("Interrupted exception during gRPC channel close", ie);
}
}
}
I was wondering if I can keep the channel open the whole time? Or do I have to open a channel every time I make a new call? I was wondering because I was testing the performance and it seems to improve drastically if I just keep the channel open. On the other hand is there something that I'm missing?
creating a new channel has huge overhead, you should keep the channel open as long as possible.
Since the opening and closing of channel is expensive I removed the channel = usePlaintext.build(); completely from my client
Instead I'm opening and closing it in my kafka Transformer. In my class UserAgentDataEnricher that implements Transformer.
public class UserAgentDataEnricher implements Transformer<byte[], EnrichedData, KeyValue<byte[], EnrichedData>> {
private UserAgentParserClient userAgentParserClient;
#Override
public void init(ProcessorContext context) {
this.context = context;
open();
// schedule a punctuate() method every 15 minutes
this.context.schedule(900000, PunctuationType.WALL_CLOCK_TIME, (timestamp) -> {
close();
open();
logger.info("Re-opening of user agent channel is initialized");
});
}
#Override
public void close() {
userAgentParserClient.shutdown();
}
private void open() {
channel = ManagedChannelBuilder.forAddress("localhost", 50051).usePlaintext().build();
userAgentClient = new UserAgentClient(channel);
}
...
}
and now I initialize my client like that:
public UserAgentClient(ManagedChannel channel) {
this.channel = channel;
userAgentBlockingStub = UserAgentServiceGrpc.newBlockingStub(channel);
}

JavaFX extends Service<> return value

I have task in my app, and i do not know how it works return from this task.
public class TimeManager extends Service<String> {
#Override
protected Task<String> createTask() {
return new Task<String>() {
#Override
protected String call() throws Exception {
String txt = null;
while (!isCancelled()) {
try {
txt = "some txt";
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
return txt;
}
};
}
And in Main Class:
TimeManager time = new TimeManager();
time.start();
time.getValue();
time allways return null. What do I have to do to return the value?
Thread works good and I can send data from the thread to the application
Your task does not publish intermediate updates. Furthermore Service is used to run tasks in background threads to avoid blocking the JavaFX application thread. For this reason the value is likely to not be assigned if you access it directly after starting the service. It would be better to use a binding or a listener to the value property to retrieve the data when it's assigned.
public class TimeManager extends Service<String> {
#Override
protected Task<String> createTask() {
return new Task<String>() {
int i = 0;
#Override
protected String call() throws Exception {
String txt = null;
while (!isCancelled()) {
txt = Integer.toString(++i);
updateValue(txt); // publish new value
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
return txt;
}
};
}
}
TimeManager time = new TimeManager();
label.textProperty().bind(time.valueProperty());
time.start();
Note that in this case a service may not be required since you're running only a single task. Running the Task instance using new Thread(task).start() may actually suffice.
Furthermore there are better options for scheduling fast repeating updates of the GUI, see JavaFX periodic background task

How can I inject a testing value for a Spring Batch configuration class?

I have a Spring Boot / Batch application and would like to write several integration tests. The batch has a FlatFileItemReader and pulls in the file to be read in via yml configuration file. Here's the batch config class:
#Configuration
#EnableBatchProcessing
public class BatchConfiguration {
#Value("${file}")
private File file;
#Bean
public ItemReader<MyClass> reader(LineMapper<MyClass> lineMapper) {
FlatFileItemReader<MyClass> flatFileItemReader = new FlatFileItemReader<MyClass>();
flatFileItemReader.setResource(new FileSystemResource(file));
final int NUMBER_OF_HEADER_LINES = 1;
flatFileItemReader.setLinesToSkip(NUMBER_OF_HEADER_LINES);
flatFileItemReader.setLineMapper(lineMapper);
return flatFileItemReader;
}
The integration test class for testing the reader is:
#SpringApplicationConfiguration(classes = LoadApplication.class)
#TestExecutionListeners( { DependencyInjectionTestExecutionListener.class,
StepScopeTestExecutionListener.class })
#RunWith(SpringJUnit4ClassRunner.class)
public class StepScopeTestExecutionListenerIntegrationTests {
#Autowired
private ItemReader<MyClass> reader;
#Rule
public TemporaryFolder testFolder = new TemporaryFolder();
#Bean
public StepExecution getStepExection() {
StepExecution execution = MetaDataInstanceFactory.createStepExecution();
return execution;
}
#Test
public void testGoodData() throws Exception {
try {
File testFile = testFolder.newFile();
PrintWriter writer = new PrintWriter(testFile, "UTF-8");
writer.println("a,b,c");
writer.println("1,2,3");
writer.close();
//ReflectionTestUtils.setField(someObject, "file", testFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
assertNotNull(reader.read());
}
}
In the above test code, what should someObject be set to? Or is there some other way to inject the test file?
The reader has already been created before the call to testGoodData. Simply override the resource that was set on the reader.
That is, replace
ReflectionTestUtils.setField(someObject, "file", testFile);
with this line of code
reader.setResource(new FileSystemResource(testFile));

What is the good way to execute JobSchedule with Quartz.Net and Asp.Net application?

I have a trouble with my ASp.net application, I have tested several solutions but I don't see any issue on it. Thanks by advance for your advice.
I have installed and running Quartz.Net as a Windows Service. Quartz.Net is in the same folder as my API in order to execute jobs in it.
Below my global.asax Application_Start
public static ISchedulerFactory schedFact;
public static IScheduler scheduler;
protected void Application_Start()
{
NameValueCollection properties = new NameValueCollection();
properties["quartz.scheduler.instanceName"] = "ServerScheduler";
properties["quartz.scheduler.proxy"] = "true";
properties["quartz.threadPool.threadCount"] = "10";
properties["quartz.scheduler.proxy.address"] = "tcp://localhost:555/QuartzScheduler";
schedFact = new StdSchedulerFactory(properties);
scheduler = schedFact.GetScheduler();
scheduler.Start();
MyAPI.Services.ScheduleTasks.JobScheduler.StartGenerateContract();
}
Here the class where my jobs are scrippted:
public class ScheduleTasks
{
public static Dictionary<string, string> report;
public class JobScheduler
{
public static void StartGenerateContract()
{
try
{
MailService.SendMail("StartGenerateContract", "Scheduletask",
new Exception(DateTime.Now.ToString()));
IJobDetail job_generate = JobBuilder.Create<GenerateAndSendContract>()
.WithIdentity("generateContractJob", "group1")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger_contracts", "group1")
.ForJob("generateContractJob", "group1")
.StartNow()
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(01, 00))
.Build();
MyAPI.MvcApplication.scheduler.ScheduleJob(job_generate, trigger);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
}
}
Then my job that is in the same file and class ScheduleTasks
public class GenerateAndSendContract : IJob
{
public void Execute(IJobExecutionContext context)
{
try
{
MailService.SendMail("GenerateAndSendContract", "Scheduletask");
//my working code...
}
catch (Exception e)
{
MailService.SendErrorMail("GenerateAndSendContract", "ScheduleTasks", e);
}
}
}
My scheduleTask is perfectly executed because I receive the first email (StartGenerateContract), with the good interval. But the job is not executed cause the code in the class generateandsendcontract is not fired (no break point, no mail GenerateAndSendContract send).
Anything wrong in my code? Thank you for your help.
When creating the scheduler on the MVC app side, you just need these properties:
properties["quartz.scheduler.instanceName"] = "RemoteClient";
properties["quartz.scheduler.proxy"] = "true";
properties["quartz.threadPool.threadCount"] = "0";
properties["quartz.scheduler.proxy.address"] = address;
You also don't need to call the Start method on the scheduler that is acting as the client.

Dependency Injection Query

I'm starting a web application that contains the following projects:
Booking.Web
Booking.Services
Booking.DataObjects
Booking.Data
I'm using the repository pattern in my data project only. All services will be the same, no matter what happens. However, if a customer wants to use Access, it will use a different data repository than if the customer wants to use SQL Server.
I have StructureMap, and want to be able to do the following:
Web project is unaffected. It's a web forms application that will only know about the services project and the dataobjects project.
When a service is called, it will use StructureMap (by looking up the bootstrapper.cs file) to see which data repository to use.
An example of a services class is the error logging class:
public class ErrorLog : IErrorLog
{
ILogging logger;
public ErrorLog()
{
}
public ErrorLog(ILogging logger)
{
this.logger = logger;
}
public void AddToLog(string errorMessage)
{
try
{
AddToDatabaseLog(errorMessage);
}
catch (Exception ex)
{
AddToFileLog(ex.Message);
}
finally
{
AddToFileLog(errorMessage);
}
}
private void AddToDatabaseLog(string errorMessage)
{
ErrorObject error =
new ErrorObject
{
ErrorDateTime = DateTime.Now,
ErrorMessage = errorMessage
};
logger.Insert(error);
}
private void AddToFileLog(string errorMessage)
{
// TODO: Take this value from the web.config instead of hard coding it
TextWriter writer = new StreamWriter(#"E:\Work\Booking\Booking\Booking.Web\Logs\ErrorLog.txt", true);
writer.WriteLine(DateTime.Now.ToString() + " ---------- " + errorMessage);
writer.Close();
}
}
I want to be able to call this service from my web project, without defining which repository to use for the data access. My boostrapper.cs file in the services project is defined as:
public class Bootstrapper
{
public static void ConfigureStructureMap()
{
ObjectFactory.Initialize(x =>
{
x.AddRegistry(new ServiceRegistry());
}
);
}
public class ServiceRegistry : Registry
{
protected override void configure()
{
ForRequestedType<IErrorLog>().TheDefaultIsConcreteType<Booking.Services.Logging.ErrorLog>();
ForRequestedType<ILogging>().TheDefaultIsConcreteType<SqlServerLoggingProvider>();
}
}
}
What else do I need to get this to work? When I defined a test, the ILogger object was null.
Perhaps some details on how you are calling this code from a test would be useful.
My understanding is that you need to ensure that the ConfigureStructureMap call has been made early in the applications life (e.g. in the Global.asax in a web project).
After that you would be calling for instances of IErrorLog using something like:
IErrorLog log = StructureMap.ObjectFactory.GetNamedInstance<IErrorLog>();

Resources